2016-07-15 7 views
0

Visual C#を使用してVisual Studio 2015コミュニティでタイムテーブルコンソールアプリケーションを作成しようとしていますが、if文が正しく評価されていません。 コンソールアプリケーションのデバッグ(テスト)を開始しましたが、最後の3つの質問でif文が正しく評価されませんでした。助けてもらえますか?ここに私のコードは次のとおりです。Visual C#でif文が正しく評価されないのはなぜですか?

あなたがする必要がある最後の三つで
using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace xTables 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
        Console.WriteLine("Welcome to xTables"); 
     Console.WriteLine("In this application, you'll have to answer questions from"); 
     Console.WriteLine("the times table up to 12"); 
     Console.WriteLine("Good Luck!"); 
     //Question 1 
     Console.WriteLine("What is 1 x 6"); 
     string userAnswer = Console.ReadLine(); 


     if (userAnswer == "6") 
     { 
      string message = "Correct"; 
      Console.WriteLine(message); 
     } 

     else 
     { 
      string message = "Incorrect"; 
      Console.WriteLine(message); 
     } 



     //Question 2 
     Console.WriteLine("What is 2 x 3"); 
     Console.ReadLine(); 

     if (userAnswer == "6") 
     { 
      string message = "Correct"; 
      Console.WriteLine(message); 
     } 

     else 
     { 
      string message = "Incorrect"; 
      Console.WriteLine(message); 
     } 



     //Question 3 
     Console.WriteLine("What is 8 x 9"); 
     Console.ReadLine(); 

     if (userAnswer == "72") 
     { 
      string message = "Correct"; 
      Console.WriteLine(message); 
     } 

     else 
     { 
      string message = "Incorrect"; 
      Console.WriteLine(message); 
     } 


     //Question 4 
     Console.WriteLine("What is 5 x 6"); 
     Console.ReadLine(); 

     if (userAnswer == "30") 
     { 
      string message = "Correct"; 
      Console.WriteLine(message); 
     } 

     else 
     { 
      string message = "Incorrect"; 
      Console.WriteLine(message); 
     } 

     //Question 5 
     Console.WriteLine("What is 4 x 6"); 
     Console.ReadLine(); 

     if (userAnswer == "24") 
     { 
      string message = "Correct"; 
      Console.WriteLine(message); 
     } 

     else 
     { 
      string message = "Incorrect"; 
      Console.WriteLine(message); 
     } 


    } 
} 

}

答えて

1

userAnswer = Console.ReadLine(); 

だけではなくConsole.ReadLine();

userAnswer = Console.ReadLine(); 

、さらなる説明のためにEDIT

への答えので:あなたはおそらくこれ欲しいとき

Console.ReadLine(); 

:あなたがこれを持っているすべてが、最初の質問では、

+0

Visual Studioによると、このスコープにはすでに 'userAnswer'が定義されています。 –

+0

@AdrianSimon偶然、「userAnswer = Console.ReadLine()」ではなく「string userAnswer = Console.ReadLine()」を入力しましたか?最初のものは、あなたが話しているエラーを引き起こす変数を再定義しようとします。 –

3

2番目の質問は、最初の質問への回答と同じです、それは動作しているように見えるかもしれません。 (あなたは同じ答えを2回入力しているかもしれませんが)実際には、動作していない最後の3つの質問だけではありません。最初の質問だけが実際にあなたが望むことをやっています。

-1

私はあなたが以下のように使うべきだと思う:

if (userAnswer.Equals(6)){ 
// Rest of the code 

} 
+0

これは参照型( 'string')と値型(' int')を比較しており、常に 'false'を返します。 'int'を' string'に変換するか、またはその逆にする必要があります。 – Cameron

0

上記の回答以外にも、あなたが本当にメッセージ変数の抽象メソッドを使用する必要はありません。

Console.WriteLine("Correct") 

はちょうど同様

0

あなたはConsole.ReadLine()の戻り値を使用する必要が働くだろう。サイドノートとして

userAnswer = Console.ReadLine(); 

、これは方法のための優れた候補である:

HandleQuestion(1, 6); 
HandleQuestion(2, 3); 
HandleQuestion(8, 9); 
HandleQuestion(5, 6); 
HandleQuestion(4, 6); 

void HandleQuestion(int operand1, int operand2) 
{ 
    Console.WriteLine("What is {0} x {1}", operand1, operand2); 
    string userAnswer = Console.ReadLine(); 

    if (userAnswer == (operand1 * operand2).ToString()) 
     Console.WriteLine("Correct"); 
    else 
     Console.WriteLine("Incorrect"); 

}

あなたはランダムな値で質問を生成するに移動することができることをやったらオペランド

0

あなたは、変数にreadline()を戻していません。 あなたがやりたいことを推測してください:userAnswer = Console.ReadLine();

関連する問題