我知道这可能是非常基本的,我用谷歌搜索并完成了搜索,但我遇到的问题是一个非常基本的if/else场景.第一个语句产生结果,这告诉我,我以某种方式弄乱了其他语句.这让我有点疯狂.仅供参考,这是我第一次使用表单窗口.
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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我要将代码块与编译器看到它的方式对齐,那么它的外观就是这样.您可以在下面的代码中看到,由于if
块没有明确的开始和结束括号,因此else
块与最接近的块耦合if
.正如彼得所说,C#中的空格并不像在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";
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以在这里添加一些花括号来解决问题.
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";
}
}
Run Code Online (Sandbox Code Playgroud)
像这样的代码的问题是2倍.1)它不是非常易读,2)它不是非常可测试的.正如Peter所指出的,通过组合下面的一些if语句,您可以轻松地降低此代码的复杂性.
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";
}
Run Code Online (Sandbox Code Playgroud)
虽然这使得代码更容易理解并减少字符串消息的重复,但我不会止步于此.为了使这段代码可以测试,我们需要删除它对点击按钮这一事实的依赖性,并且可能是一个涉及的表单组件(label3
).为此,我们可以将此代码块移动到返回字符串的方法,而不是设置字符串.
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";
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我们将代码与输入和显示数据所涉及的表单分离开来.我们通过消除对else语句的需求,进一步降低了复杂性.通过从方法返回字符串,我们退出代码块,因为不需要继续执行if检查.