C#如果/其他第一条语句正常工作,其余的则不行 [英] C# if/else first statement working, the rest are not

查看:45
本文介绍了C#如果/其他第一条语句正常工作,其余的则不行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是非常基本的,我用它搜索了一下并进行了搜索,但是遇到一个非常基本的if/else场景问题.第一条语句产生结果,这告诉我我已经以某种方式弄乱了其他语句.这让我有些疯狂.仅供参考,这是我第一次使用表单窗口.

I know this is probably very basic, and I googled it and have gone through the search, but I'm having an issue which a very basic if/else scenario. The first statement produces results, which tells me that I've messed up the other statements somehow. It's driving me a little crazy. FYI, this is my first experience with a form window.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Wk2_Part1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {  
            int season;
            int job;
            season = Convert.ToInt32(textBox1.Text);
            job = Convert.ToInt32(textBox2.Text);
            if (season == 1)
                if (job == 1)
                    label3.Text = "There is a 20% discount on the exterior job";
            else
                if (season == 2)
                    if (job == 1)
                        label3.Text = "There is a 20% discount on the exterior job";
            else
                if (season == 3)
                    if (job == 2)
                        label3.Text = "There is a 30% discount on the interior job";
            else
                 label3.Text = "No discount, regular prices apply";
        }
    }
 }

推荐答案

如果我要使您的代码块与编译器如何看待对齐,它就是它的外观.您可以在下面的代码中看到,由于 if 块没有明确的开始和结束括号,因此 else 块与最接近的先前 if .正如Peter所说,C#中的空格与python中的空格没有关系.

If I were to align your code block with how the compiler sees it, this is how it would look. You can see in the code below that since there are no explicit start and ends brackets for your if blocks that the else blocks are coupled with the closest previous if. As Peter said, the white space in C# does not matter like it does in python.

    private void button1_Click(object sender, EventArgs e)
    {  
        int season;
        int job;
        season = Convert.ToInt32(textBox1.Text);
        job = Convert.ToInt32(textBox2.Text);
        if (season == 1)
            if (job == 1)
                label3.Text = "There is a 20% discount on the exterior job";
            else
                if (season == 2)
                    if (job == 1)
                        label3.Text = "There is a 20% discount on the exterior job";
                    else
                        if (season == 3)
                            if (job == 2)
                                label3.Text = "There is a 30% discount on the interior job";
                            else
                                label3.Text = "No discount, regular prices apply";
    }

现在,我们可以在此处添加一些花括号来解决该问题.

Now we could add some curly braces here to fix the issue.

    private void button1_Click(object sender, EventArgs e)
    {  
        int season;
        int job;
        season = Convert.ToInt32(textBox1.Text);
        job = Convert.ToInt32(textBox2.Text);
        if (season == 1) 
        {
            if (job == 1)
            {
                label3.Text = "There is a 20% discount on the exterior job";
            }
        }
        else
        {
            if (season == 2)
            {
                if (job == 1)
                {
                    label3.Text = "There is a 20% discount on the exterior job";
                }
            }
        }
        else
        {
            if (season == 3)
            {
                if (job == 2)
                {
                    label3.Text = "There is a 30% discount on the interior job";
                }
            }
        }
        else
        {
             label3.Text = "No discount, regular prices apply";
        }
    }

像这样的代码的问题是2折.1)它不是很可读,并且2)它不是很可测试.正如Peter指出的那样,您可以通过组合以下一些if语句来轻松降低此代码的复杂性.

The problem with code like this is 2 fold. 1) it is not very readable, and 2) it is not very testable. As Peter also pointed out you can easily reduce the complexity of this code by combining some of the if statements as below.

if ((season == 1 || season == 2) && job == 1)
{
    label3.Text = "There is a 20% discount on the exterior job";
}
else if (season == 3 && job == 2)
{
    label3.Text = "There is a 30% discount on the interior job";
}
else
{
    label3.Text = "No discount, regular prices apply";
}

尽管这使代码更易于理解,并减少了字符串消息的重复,但我不会在这里停下来.为了使此代码可测试,我们需要删除其对以下事实的依赖:单击按钮,并且可能涉及表单组件( label3 ).为此,我们可以将这段代码移到返回字符串而不是设置字符串的方法中.

While this makes the code much easier to understand and reduces the duplication of the string messages, I would not stop here. In order for this code to be testable we need to remove the dependency it has on the fact that there is a button click, and presumably a form component involved (label3). To do that we can move this block of code to a method that returns a string, rather than sets a string.

private void button1_Click(object sender, EventArgs e)
{  
    int season = Convert.ToInt32(textBox1.Text);
    int job = Convert.ToInt32(textBox2.Text);

    label3.Text = GetDiscount(season, job);
}

private String GetDiscount(int season, int job)
{
    if ((season == 1 || season == 2) && job == 1)
    {
        return "There is a 20% discount on the exterior job";
    }

    if (season == 3 && job == 2)
    {
        return "There is a 30% discount on the interior job";
    }

    return "No discount, regular prices apply";
}

通过这种方式,我们已经将代码与输入和显示数据所涉及的形式分离了.通过消除对else语句的需要,我们还进一步降低了复杂性.通过从该方法返回字符串,我们退出了代码块,因为无需继续执行if检查.

In this manner we have decoupled the code from the form that is involved in inputing and displaying the data. We have also reduced the complexity even more by eliminating the need for the else statements. By returning the string from the method we exit the block of code as there is no need to continue performing the if checks.

这篇关于C#如果/其他第一条语句正常工作,其余的则不行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆