2016-05-16 46 views
0

我是一個編程noob,我似乎無法讓這個計算器正常工作。我已經編寫了所有的按鈕,並允許用戶從鍵盤輸入(我沒有放入代碼摘錄,因爲它的無關緊要),但正如標題所暗示的那樣,等於按鈕不會有多於一個的總和。我不確定是什麼導致了這個問題。任何建議,將不勝感激。簡單的計算器禍(無法讓等號按鈕多次工作)

這是我的代碼。

public partial class MainWindow : Window 
{ 
    string input = string.Empty;   //String storing user input 
    string operand1 = string.Empty;   //String storing first operand 
    string operand2 = string.Empty;   //String storing second operand 
    char operation;       //char for operarion 
    double result = 0.0;     //calculated result 
    bool operationCompleted = false; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void btn_Zero_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "0"; 
     this.textBox.Text += input; 

     if(operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_One_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "1"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Two_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "2"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Three_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "3"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 


    private void btn_Four_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "4"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      input = string.Empty; 
      textBox.Text = string.Empty; 
      operand1 = string.Empty; 
      operand2 = string.Empty; 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Five_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "5"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Six_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "6"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Seven_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "7"; 
     this.textBox.Text += input; 
     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 

    } 

    private void btn_Eight_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "8"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Nine_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "9"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Dot_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "."; 
     this.textBox.Text += input; 
     btn_Equals.Focus(); 
    } 

    private void btn_Minus_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '-'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Plus_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '+'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Multiply_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '*'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Divide_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '/'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 

    } 


    // The equals works for the first sum but not for any after it 

    private void btn_Equals_Click(object sender, RoutedEventArgs e) 
    { 
     { 
      operand2 = input; 
      double num1, num2; 
      double.TryParse(operand1, out num1); 
      double.TryParse(operand2, out num2); 

      if (operation == '+') 
      { 
       result = num1 + num2; 
       textBox.Text = result.ToString(); 
      } 

      else if (operation == '-') 
      { 
       result = num1 - num2; 
       textBox.Text = result.ToString(); 
      } 
      else if (operation == '*') 
      { 
       result = num1 * num2; 
       textBox.Text = result.ToString(); 
      } 
      else if (operation == '/') 
      { 
       if (num1 != 0 || num2 != 0) 
       { 
        result = num1/num2; 
        textBox.Text = result.ToString(); 
       } 

       else 
       { 
        textBox.Text = "Cannot divide by zero"; 
       } 

      } 
      operationCompleted = true; 
      this.Focus(); 
     } 
    } 

    private void btn_Clear_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     this.input = string.Empty; 
     this.operand1 = string.Empty; 
     this.operand2 = string.Empty; 
     this.result = 0.0; 
     operationCompleted = false; 
    } 

} 
+0

我認爲這與你的'operation'變量有關。你聲明它是一個'char',但是你每次按下一個操作符按鈕就會添加它,這會「破壞」它的狀態。你應該使用'='而不是'+ ='或者在計算之間清除它。 –

回答

2

您需要清除操作。你有操作+ ='/'。所以每次有人點擊其中一個操作按鈕時,您都會將該操作附加到字符串。當你點擊等號按鈕時,你不會有錯誤的操作,所以沒有任何反應。

建議:

清除完成操作。 如果考慮到錯誤的操作,則添加默認值,以便處理錯誤。

+0

謝謝修復:D –