考虑这个片段:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.MaxLength > 0)
{
textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
}
else if (textBox1.MaxLength < 32768)
{
textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
}
}
为什么不评估第二个条件(小于条件)?这也是事实,不是吗?
如果我必须让第二个在相同条件下工作,必须做哪些小改动?
最佳答案
没有检查第二个条件,因为只有当前面的条件被评估为 false
时,才会评估 else
语句。有几种方法可以解决此问题,其中一些方法已在其他答案中显示。一种这样的方法是使用单个 if
语句,它结合了两个条件:
if (textBox1.MaxLength > 0 && textBox1.MaxLength < 32768)
{
textBox2.Text = Convert.ToBoolean(Math.Sqrt(textBox1.MaxLength)).ToString();
}
关于c# - 在 if 条件为真之后评估 else if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491834/