2015-03-18 88 views
0

我是新來的C#,所以請善待。我正在爲我的編程課程進行任務,並且不能在我的生活中找出問題所在。該程序是一個計算器,它接受來自兩個文本框的輸入並對其進行操作,並禁用第二個文本框並使用按鈕輸入數字(數字鍵盤)。我正在使用Visual Studio 2013,並多次遍歷我的代碼並逐步完成。這裏是我的代碼:解析文本時發生System.FormatException

static int checkFocusedTextbox = 1; 
    string operationPerformed; 
    double result = 0; 

    private void Operator_Click(object sender, RoutedEventArgs e) 
    { 
     // try 
     // { 
      Button button = (Button)sender; 
      operationPerformed = button.Content.ToString(); 
      double num1 = double.Parse(inputOne.Text); 
      double num2 = double.Parse(inputTwo.Text); 

      if (checkFocusedTextbox == 1) 
      { 
       if ((inputOne.Text == "") || (inputTwo.Text == "")) 
       { 
        error.Text = "Please fill both text fields"; 
       } 
       else if ((inputOne.Text != "") || (inputTwo.Text != "")) 
       { 
        switch ((string)button.Tag) 
        { 
         case "+": 
          inputOne.Clear(); 
          inputTwo.Focus(); 
          result = num1 + num2; 
          break; 
         case "-": 
          inputOne.Clear(); 
          num2 = double.Parse(inputOne.Text); 
          result = num1 + num2; 
          break; 
         case "*": 
          inputOne.Clear(); 
          num2 = double.Parse(inputOne.Text); 
          result = num1 + num2; 
          break; 
         case "/": 
          inputOne.Clear(); 
          num2 = double.Parse(inputOne.Text); 
          result = num1/num2; 
          break; 
         case "%": 
          inputOne.Clear(); 
          num2 = double.Parse(inputOne.Text); 
          result = num1 % num2; 
          break; 
         default: 
          break; 
        } 
        output.Text = result.ToString(); 
       } 
      } 
      else 
      { 
       Calculation(num1, num2); 
       output.Text = result.ToString(); 
      } 
    } 

    public void Calculation(double number1, double number2) 
    { 
     switch (operationPerformed) 
     { 
      case "+": 
       result = number1 + number2; 
       break; 
      case "-": 
       result = number1 - number2; 
       break; 
      case "*": 
       result = number1 * number2; 
       break; 
      case "/": 
       result = number1/number2; 
       break; 
      case "%": 
       result = number1 % number2; 
       break; 
      default: 
       break; 
     } 
    } 

    public void CalculationNumberpad() 
    { 

    } 

    private void NumberPad_Click(object sender, RoutedEventArgs e) 
    { 
     Button button = (Button)sender; 
     inputOne.Text += button.Content.ToString(); 
    } 

    private void inputTwo_GotFocus(object sender, RoutedEventArgs e) 
    { 
     checkFocusedTextbox = 2; 
    } 

    private void inputOne_GotFocus(object sender, RoutedEventArgs e) 
    { 
     checkFocusedTextbox = 1; 
    } 
} 

}

Button類成功地檢索按鈕點擊頭到operationPerformed變量。這已經在我的代碼的頂部作爲字符串聲明,所以我不明白爲什麼我使用ToString()後出現錯誤。然後進入num1 double並給出錯誤。

我迫切需要幫助,我知道這個社區是最好的!如果我遺漏了任何信息,請原諒我。

謝謝, 克里斯

回答

0

你這樣做:

double num1 = double.Parse(inputOne.Text); 
    double num2 = double.Parse(inputTwo.Text); 

您檢查之前inputOne或inputTwo的內容是否爲空或不。如果其中任一個爲空或者包含不能被解析爲雙精度的字符串,則解析線會拋出System.FormatException異常。

看看double.TryParse來檢查是否有可分析的東西,而不會引發異常。因此,像:

double num1, num2; 
if (double.TryParse(inputOne.Text, out num1) && double.TryParse(inputTwo.Text, out num2)) 
{ ... values are valid, so do things} 
else 
{ ... at least one value is not valid... warn user } 
0

這些條件是錯誤的: (!inputOne.Text = 「」)|| (inputTwo.Text!=「」)

要測試字符串是否相等,請使用str1.Equals(str2); 也與A ||相反B is!A & &!B

更好的測試是string.IsNullorEmpty(teststring);

0

你在這裏做這個

inputOne.Clear(); // this makes the TextBox empty 

那麼這

num2 = double.Parse(inputOne.Text); // How can you parse an empty text 

變化像

case "-": 
    num2 = double.Parse(inputOne.Text); // First take the value 
    inputOne.Clear();     // Then clear 
    result = num1 + num2;    // Then Calculate 
    break; 

而對於這裏的比較

else if ((inputOne.Text != "") || (inputTwo.Text != "")) 

您已經證實,這兩個文本框填寫,那麼這將永遠是true,所以只是一個簡單的其他就足夠了。

else // that's enough 

替換行。

雖然從按鈕取籤運營商這裏

switch ((string)button.Tag) 

它不是標籤需要。你需要的是按鈕

改變,要

switch ((string)button.Content.ToString()) 

內容

如何填寫inputTwo TextBox?

Button button = (Button)sender; 
inputOne.Text += button.Content.ToString(); 

這裏你唯一的選擇,以填補inputOne

所以更改喜歡這裏

Button button = (Button)sender; 
if (checkFocusedTextbox == 1) 
    inputOne.Text += button.Content.ToString(); 
else 
    inputTwo.Text += button.Content.ToString();