C# 错误检查问题
在大多数情况下,这是可行的,问题是Andrea和Brittany的消息框会弹出,但Eric的消息框正常。如果我试着把else语句放在每个If语句之后,它仍然会在Brittany和Andrea上弹出,但在Eric上也会弹出。有人能告诉我我做错了什么吗C# 错误检查问题,c#,error-checking,C#,Error Checking,在大多数情况下,这是可行的,问题是Andrea和Brittany的消息框会弹出,但Eric的消息框正常。如果我试着把else语句放在每个If语句之后,它仍然会在Brittany和Andrea上弹出,但在Eric上也会弹出。有人能告诉我我做错了什么吗 private void button1_Click(object sender, EventArgs e) { String Andrea; String Brittany; Stri
private void button1_Click(object sender, EventArgs e)
{
String Andrea;
String Brittany;
String Eric;
if (textBox1.Text == "Andrea")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
if (textBox1.Text == "Brittany")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
if (textBox1.Text == "Eric")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
{
}
}
如果像这样,试着用一个else
if (textBox1.Text == "Andrea")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Brittany")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Eric")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
如果像这样,试着用一个else
if (textBox1.Text == "Andrea")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Brittany")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Eric")
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
试试这个。。。通过保留名称列表,您可以轻松地扩展所涵盖的名称,而无需编写更多代码。只需将新名称添加到名称列表中即可
List<string> names = new List<string>() // list of names to check for
{ // if a name is not in this list
"Andrea","Brittany","Eric" // the error message will show
}; // otherwise, the calculation will be performed
if ( names.Contains(TextBox1.Text) )
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
List name=new List()//要检查的名称列表
{//如果名称不在此列表中
“Andrea”、“Brittany”、“Eric”//将显示错误消息
}; // 否则,将执行计算
if(names.Contains(TextBox1.Text))
{
Commission.Text=(Convert.ToDouble(textBox2.Text)/10.ToString();
}
其他的
{
MessageBox.Show(“名称拼写不正确”、“拼写错误”);
}
试试这个。。。通过保留名称列表,您可以轻松地扩展所涵盖的名称,而无需编写更多代码。只需将新名称添加到名称列表中即可
List<string> names = new List<string>() // list of names to check for
{ // if a name is not in this list
"Andrea","Brittany","Eric" // the error message will show
}; // otherwise, the calculation will be performed
if ( names.Contains(TextBox1.Text) )
{
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
List name=new List()//要检查的名称列表
{//如果名称不在此列表中
“Andrea”、“Brittany”、“Eric”//将显示错误消息
}; // 否则,将执行计算
if(names.Contains(TextBox1.Text))
{
Commission.Text=(Convert.ToDouble(textBox2.Text)/10.ToString();
}
其他的
{
MessageBox.Show(“名称拼写不正确”、“拼写错误”);
}
好吧,我不知道您以前的尝试,但目前每个if语句都是单独处理的。所以如果textBox1.Text!=“Eric”,附加到Eric的else将被触发,在本例中,显示MessageBox,不管其他两个if是否匹配
也许你的else if尝试有错误?试试上面的一些人是如何发布的,看看是否有效。好吧,我不知道你以前的尝试,但目前每个if语句都是单独处理的。所以如果textBox1.Text!=“Eric”,附加到Eric的else将被触发,在本例中,显示MessageBox,不管其他两个if是否匹配
switch(textBox1.Text)
{
case "Andrea" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
case "Brittany" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
case "Eric" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
default: MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
也许你的else if尝试有错误?试试上面一些人的发帖方式,看看是否有效。这是最好的。我还有一个问题,do while循环完成同样的任务会是什么样子。do…while什么?这取决于你想要完成什么。这是最好的。我还有一个问题,do while循环完成同样的任务会是什么样子。do…while什么?这将取决于你想要完成什么。+1,但实际上我们在重复自己。希望我们能把它瘦得更远。+1,但实际上我们在重复自己。希望我们能把它瘦得更远。
switch(textBox1.Text)
{
case "Andrea" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
case "Brittany" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
case "Eric" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
default: MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}