2017-02-14 80 views
0

我是控制檯應用程序的新手,我通常使用C#for Unity。該代碼並不真正如何我想要它嘗試做數學方程時出錯

是的我知道使用轉到不好。但我不知道替代方案

我有[a = 2] [b = 3]和[ans = a + b],所以顯而易見的答案是5.因此,當你放5它運行Else語句是弄錯了。

 goto start; 
     error: 
     Console.Clear(); 
     Console.WriteLine("Input not Recognized"); 
     Console.WriteLine("Try Again"); 
     Console.WriteLine("\nType (Reset) to Reset Program"); 
     Console.WriteLine("\nType (End) to End Program"); 
     Console.WriteLine(""); 
     string error1 = Console.ReadLine(); 
     if (error1.Equals("reset", StringComparison.InvariantCultureIgnoreCase)) 
     { 
      goto start; 
     } 
     if (error1.Equals("end", StringComparison.InvariantCultureIgnoreCase)) 
     { 
      Environment.Exit(0); 
     } 
     else 
     { 
      goto error; 
     } 
     start: 
     Console.WriteLine("Solve the Math Equation"); 
     int a = 2; 
     int b = 3; 
     int ans = a + b; 
     Console.WriteLine("\n2 + 3"); 
     Console.WriteLine(""); 
     string user = ""; 
     ConsoleKeyInfo key; 

     do 
     { 
      key = Console.ReadKey(true); 
      if (key.Key != ConsoleKey.Backspace) 
      { 
       double val = 0; 
       bool _x = double.TryParse(key.KeyChar.ToString(), out val); 
       if (_x) 
       { 
        user += key.KeyChar; 
        Console.Write(key.KeyChar); 
       } 
      } 
      else 
      { 
       if (key.Key == ConsoleKey.Backspace && user.Length > 0) 
       { 
        user = user.Substring(0, (user.Length - 1)); 
        Console.Write("\b \b"); 
       } 
      } 
     } 
     while (key.Key != ConsoleKey.Enter); 
     if (user.Equals(ans)) 
     { 
      Console.Clear(); 
      Console.WriteLine("Correct!"); 
      Console.WriteLine("\nYour answer " + ans); 
      Console.WriteLine("\nType (End) to End Program"); 
      Console.WriteLine(""); 
      string end1 = Console.ReadLine(); 
      if (end1.Equals("end", StringComparison.InvariantCultureIgnoreCase)) 
      { 
       Environment.Exit(0); 
      } 
      else 
      { 
       goto error; 
      } 
     } 
     else 
      { 
       Console.Clear(); 
       Console.WriteLine("Incorrect!"); 
       Console.WriteLine("\nThe answer was " + ans); 
       Console.WriteLine("\nType (Reset) to Reset Program"); 
       Console.WriteLine("Type (End) to End Program"); 
       Console.WriteLine(""); 
       string rne1 = Console.ReadLine(); 
       if (rne1.Equals("reset", StringComparison.InvariantCultureIgnoreCase)) 
       { 
       Console.Clear(); 
        goto start; 
       } 
       if (rne1.Equals("end", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        Environment.Exit(0); 
       } 
       else 
        goto error; 

回答

1

userans是不是在你的代碼,這就是爲什麼你的代碼跳轉到錯誤相等。他們不相同的原因是他們的類型。

user是一個字符串 ans是一個整數

那麼,你是比較「5」爲5,可以不相等。

轉換其中一個變量,使其具有相同的類型。

在if語句中使用user.Equals(ans.ToString())或將字符串轉換爲數字(這是更好的解決方案IMO - 因爲它也處理輸入不是數字時的情況)。就像這樣:

int userAns; 
if (!Int32.TryParse(user, userAnsj)) 
    Console.WriteLine("Input is not a valid integer."); 

,然後comapare userAnsans