2017-02-14 25 views
0

私はコンソールアプリケーションを初めて使用しています。通常、UnityにはC#を使用しています。コードは実際に私が望む方法では動作しません数式を実行しようとするとエラーが発生します

はい私は後藤を使用することは良くありません。しかし、私は選択肢を知らない

私は[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."); 

、その後はansからuserAnsをcomapare。

関連する問題