如果语句正常工作,则无法变得简单

时间:2014-02-21 17:45:44

标签: c#

问题在于,无论我尝试alert,只有在 BOTH 为空时才会出现。如果其中一个人有值,则会将resOne.Text设置为F

以下是代码:

private void btn_calculate_Click(object sender, EventArgs e)
    {
        // START OF AND OPERATION
        if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")
        {
            if (valueOne.Text == "T" || valueOne.Text == "1")
            {
                if (valueTwo.Text == "T" || valueTwo.Text == "1")
                {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
                }
                else
                {
                    resOne.Text = "F";
                    resOne.BackColor = Color.Salmon;
                    resLineOne.BackColor = Color.Salmon;
                }
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }
        }
        else if (valueOne.Text == "" || valueTwo.Text == "")
        {
            MessageBox.Show("Error: Empty Fields");
        }
    }

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

这一行是主要问题

if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")

我假设您的comboBox.Text始终设置为“AND”

然后 什么时候 valueOne.Text EMPTY,valueTwo.Text NON-EMPTY 你完成了OR的第二部分

valueTwo.Text != ""

当 valueOne.Text NON-EMPTY ,valueTwo.Text EMPTY 你完成了OR的第一部分

comboBox1.Text == "AND" && valueOne.Text != ""

因此,即使任一个输入,也无法转到提醒消息。 应该是

if (comboBox1.Text == "AND" && valueOne.Text != "" && valueTwo.Text != "")

ELSE IF 仅在 IF 表达式 FALSE

之后才进行表达式检查

答案 1 :(得分:0)

if (valueOne.Text == "" || valueTwo.Text == "")

   MessageBox.Show("Error: Empty Fields");
}
else{
   // do something 

}

答案 2 :(得分:0)

首先,我们就是这样。

private void btn_calculate_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")
        {
            //SO This bit assumes everything has a value in
            if (valueOne.Text == "T" || valueOne.Text == "1" || valueTwo.Text == "T" || valueTwo.Text == "1)
            {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }
        }
        else if (valueOne.Text == "" || valueTwo.Text == "")
        {
            MessageBox.Show("Error: Empty Fields");
        }
    }

你是如何设置文本框的?那可能是你的问题。如果它们不完全是T1,则会导致问题。可能值得尝试Trim()方法以确保没有空格。这不是IF声明的问题。

如果它将文本框设置为“F”,则以下内容没有任何问题:

if (comboBox1.Text == "AND" && valueOne.Text != "" || valueTwo.Text != "")

您的错误发生在此处:

if (valueOne.Text == "T" || valueOne.Text == "1" || valueTwo.Text == "T" || valueTwo.Text == "1)

我建议调试它并逐步完成:)

答案 3 :(得分:0)

首先尝试确定空字段。我还会使用String.IsNullOrEmpty来检查空字符串

    if (String.IsNullOrEmpty(valueOne.Text) || String.IsNullOrEmpty(EmptyvalueTwo.Text))
    {
        MessageBox.Show("Error: Empty Fields");
    }        
    else
    {
        if (comboBox1.Text.equals("AND"))
        {
            if (valueOne.Text.equals("T") || valueOne.Text.equals("1"))
            {
                if (valueTwo.Text.equals("T") || valueTwo.Text.equals("1"))
                {
                    resOne.Text = "T";
                    resOne.BackColor = Color.LawnGreen;
                    resLineOne.BackColor = Color.LawnGreen;
                }
                else
                {
                    resOne.Text = "F";
                    resOne.BackColor = Color.Salmon;
                    resLineOne.BackColor = Color.Salmon;
                }
            }
            else
            {
                resOne.Text = "F";
                resOne.BackColor = Color.Salmon;
                resLineOne.BackColor = Color.Salmon;
            }   
        }
    }