2012-01-31 15 views
3

コンピューターが1〜100の数値を考える乱数推測ゲームを作っています。その後、それが何であるかを尋ね、あなたが正しいか間違っているかをあなたに伝えます。しかし、私がデバッグするたびに、それは何らかの理由で実際の乱数よりも高いか低いという。プラス、それはすぐにそれらのステートメントの2つを言う。また、私は人がどれくらい多くの推測をしたかについてはわかりません。ここで私の失敗したコードです。乱数推測ゲーム

static void Main(string[] args) 
{ 
    Random random = new Random(); 

    int returnValue = random.Next(1, 100); 
    int Guess = 0; 

    Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

    while (Guess != returnValue) 
    { 
     Guess = Convert.ToInt32(Console.Read()); 

     while (Guess < returnValue) 
     { 
      Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
      Console.ReadLine(); 

     } 
     while (Guess > returnValue) 
     { 
      Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
      Console.ReadLine(); 
     } 
    } 
    while (Guess == returnValue) 
    { 
     Console.WriteLine("Well done! The answer was " + returnValue); 
     Console.ReadLine(); 
    } 
} 
+0

問題は何ですか? – Ben

+7

あなたはおそらくifsが必要な場所をいくつか持っています。 –

+0

あなたは毎回 'while'ループに落ちています。そのため、複数のプロンプトが表示されます。一致を見つけたら 'while'ループから抜け出すことができますが、内側' while'ループを 'if'文で置き換えるほうがよいでしょう。 –

答えて

5

。 whileステートメントは、IFステートメントと同様にブール条件をとります。

static void Main(string[] args) 

{ 

Random random = new Random(); 

int returnValue = random.Next(1, 100); 

     int Guess = 0; 

     Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

     while (Guess != returnValue) 
     { 
      Guess = Convert.ToInt32(Console.Read()); 

      if (Guess < returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?"); 
      } 
      else if (Guess > returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?"); 
      } 

     } 

     Console.WriteLine("Well done! The answer was " + returnValue); 
     Console.ReadLine(); 

} 
+1

最後の 'if'は廃止されました。なぜなら、' Guess == returnValue'のときだけループが終了するからです! –

+2

最初に正しく推測すると、「それは何かを推測できますか?」という質問が表示されます。再び。 – Phil

+0

Philさん、ありがとうございました。答えが正しくない場合のみ出力するよう修正しました。 – OnResolve

0

代わりwhileifのを作ってみましょう。以下のような:それは各プロンプトの前にあなたを求め続けるようwhileループ内

Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

プロンプト:

if (Guess < returnValue) 
{ 
    Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
} 
if (Guess > returnValue) 
{ 
    Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
} 

また、置きたいかもしれません。

0

は、あなたはしばらくの間は限り文が真であるように、そのコードを実行する場合-THEN-ELSE文

にごWhileループを変更する必要があります。 あなたのコードでは、条件のいずれかの値をリセットしていないので、最初のものを実行します。

whileループを終了すると、他のwhileループと同じ問題が発生します。 あなたはこのような何かをしたい:あなたが不要な反復のたくさんを使用している

if (guess > myValue) { // do something } 
else (guess < myValue) {//do something else} 
else { // do a third thing } 
1

ロジックを再構成して、必要なものを正確に実行してください。他の人が言ったように

Random r = new Random(); 

int val = r.Next(1, 100); 
int guess = 0; 
bool correct = false; 

Console.WriteLine("I'm thinking of a number between 1 and 100."); 

while (!correct) 
{ 
    Console.Write("Guess: "); 
    string input = Console.ReadLine(); 

    if (!int.TryParse(input, out guess)) 
    { 
     Console.WriteLine("That's not a number."); 
     continue; 
    } 

    if (guess < val) 
    { 
     Console.WriteLine("No, the number I'm thinking is higher than that number."); 
    } 
    else if (guess > val) 
    { 
     Console.WriteLine("No, the number I'm thinking is lower than that number."); 
    } 
    else 
    { 
     correct = true; 
     Console.WriteLine("You guessed right!"); 
    } 
} 
0

、あなたはifが本当にneeededさwhileを悪用されています。

static void Main(string[] args) 
{ 

    Random random = new Random(); 

    int returnValue = random.Next(1, 100); 
    int Guess = 0; 
    int numGuesses = 0; 

    Console.WriteLine("I am thinking of a number between 1-100. Can you guess what it is?"); 

    while (Guess != returnValue) 
    { 
     Guess = Convert.ToInt32(Console.Read()); 
     string line = Console.ReadLine(); // Get string from user 
     if (!int.TryParse(line, out Guess)) // Try to parse the string as an integer 
      Console.WriteLine("Not an integer!"); 
     else { 
      numGuesses++; 
      if (Guess < returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is higher than " + Guess + " . Can you guess what it is?"); 
      } 
      if (Guess > returnValue) 
      { 
       Console.WriteLine("No, the number I am thinking of is lower than " + Guess + " . Can you guess what it is"); 
      } 
     } 
    } 
    Console.WriteLine("Well done! The answer was " + returnValue + ".\nYou took " + numGuesses + " guesses."); 
} 
0

おい...

int total = 1, 
      low = 0, 
      high = 0; 
     int ranNum1, 
      guess; 

     string guessStr; 

     Random ranNumGen = new Random(); 
     ranNum1 = ranNumGen.Next(1, 10); 

     Console.Write("Enter your guess >> "); 
     guessStr = Console.ReadLine(); 
     guess = Convert.ToInt16(guessStr); 

     while (guess != ranNum1) 
     { 
      while (guess < ranNum1) 
      { 
       Console.WriteLine("Your guess is to low, try again."); 
       Console.Write("\nEnter your guess >> "); 
       guessStr = Console.ReadLine(); 
       guess = Convert.ToInt16(guessStr); 
       ++total; 
       ++low; 
      } 
      while (guess > ranNum1) 
      { 
       Console.WriteLine("Your guess is to high, try again."); 
       Console.Write("\nEnter your guess >> "); 
       guessStr = Console.ReadLine(); 
       guess = Convert.ToInt16(guessStr); 
       ++total; 
       ++high; 
      } 
     } 
     //total = low + high; 
     Console.WriteLine("It took you {0} guesses to correctly guess {1}", total, ranNum1); 
0

(1と9を含む)1と9の間の乱数を発生させます。ユーザーに数字を聞かせて、あまりにも低いか高いか、正確に正しいかどうかを教えてください。エクストラ:ユーザの入力が終了するまでゲームを続けると、ユーザーが推測した推測の数が記録されます。ゲームが終了したら、これを印刷します。

関連する問題