为什么说无效表达?

时间:2011-07-04 05:54:34

标签: c#

为什么这不起作用?

private void btnEquals_Click(object sender, EventArgs e)
{
    if (plusButtonClicked == true)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
    }
    else if (minusButtonClicked == true);
    {
        total2 = total1 - double.Parse(txtDisplay.Text);
    }
    else if (multiplyButtonClicked == true);
    {
        total2 = total1 * double.Parse(txtDisplay.Text);
    }
    else
    {
        total2 = total1 / double.Parse(txtDisplay.Text);
    }

直到第二个“else if”它表示无效的表达式

5 个答案:

答案 0 :(得分:5)

因为你在第一个else-if行之后加了一个分号。

private void btnEquals_Click(object sender, EventArgs e)
{
    if (plusButtonClicked == true)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
    }
    else if (minusButtonClicked == true);  // <<== There should not be a semicolon here
    {
        total2 = total1 - double.Parse(txtDisplay.Text);
    }
    else if (multiplyButtonClicked == true); // <<== or here
    {
        total2 = total1 * double.Parse(txtDisplay.Text);
    }
    else
    {
        total2 = total1 / double.Parse(txtDisplay.Text);
    }

答案 1 :(得分:1)

在else之后删除分号

private void btnEquals_Click(object sender, EventArgs e)
{
    if (plusButtonClicked == true)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
    }
        else if (minusButtonClicked == true)
    {
        total2 = total1 - double.Parse(txtDisplay.Text);
    }
        else if (multiplyButtonClicked == true)
    {
        total2 = total1 * double.Parse(txtDisplay.Text);
    }
        else
    {
        total2 = total1 / double.Parse(txtDisplay.Text);
    }
  }

答案 2 :(得分:1)

在else-if

之后删除分号
 else if (minusButtonClicked == true);  //remove semicolon

无需==true中使用(minusButtonClicked == true)

答案 3 :(得分:0)


private void btnEquals_Click(object sender, EventArgs e) { 
            if (plusButtonClicked == true) 
            { 
                total2 = total1 + double.Parse(txtDisplay.Text); 
            }
            else if (minusButtonClicked == true) 
            { 
                total2 = total1 - double.Parse(txtDisplay.Text); 
            }
            else if (multiplyButtonClicked == true)
            { 
                total2 = total1 * double.Parse(txtDisplay.Text); 
            } 
            else
            { 
                total2 = total1 / double.Parse(txtDisplay.Text);
            }
        }

答案 4 :(得分:0)

在if-else威胁之后分号的含义你只是停止了这个条件。

删除;

并添加:

private void btnEquals_Click(object sender, EventArgs e)
{
    if (plusButtonClicked == true)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
    }
        else if (minusButtonClicked == true)
    {
        total2 = total1 - double.Parse(txtDisplay.Text);
    }
        else if (multiplyButtonClicked == true)
    {
        total2 = total1 * double.Parse(txtDisplay.Text);
    }
        else
    {
        total2 = total1 / double.Parse(txtDisplay.Text);
    }
  }