2011-07-04 82 views
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); 
    } 

它的工作原理,直到第二個「否則,如果」它說無效表達其他

+2

無關,但作爲一個風格問題,考慮避免'如果(布爾==真)'。相反,寫'if(boolean)'。 – user122211

+0

這是一個無用的代碼段來調試問題。 'txtDisplay','total *'等等的類型是什麼......請詳細說明..究竟是什麼錯誤,你如何調試代碼? – Arpit

回答

5

因爲你把一個分號第一否則,如果行之後。

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

刪除分號後,如果是

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); 
    } 
    } 
1

刪除分號後還有其他人,如果

else if (minusButtonClicked == true); //remove semicolon 

無需(minusButtonClicked == true)

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); 
      } 
     } 

使用
+0

thankyouthankyouthankyouthankyouthankyouthankyou !!!! – matthew

+0

其他問題... – matthew

+0

private void btnPlus_Click(object sender,EventArgs e) { total1 = total1 + double.Parse(txtDisplay.Text); txtDisplay.Clear(); plusButtonClicked = true; minusButtonClicked = false; divideButtonClicked = false; multiplyButtonClicked = false; – matthew

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); 
    } 
    } 
+0

謝謝大家:) – matthew

相關問題