2016-05-05 5 views
0

私は、プレイした後に再度プレイしたいと思った場合、ユーザーに尋ねるシンプルな低ローゲームを開発しようとしています。外側のWhileループを削除すると、内側のループのロジックが正確にやりたいことができますが、内側のループを外側のループで囲む方法がわかりません。これは、再生を再質問します。それらを内側のループに戻します。以下は私のコードです。私の低から高のゲームを繰り返すように見えない

import java.util.Scanner; 
import java.util.Random; 

public class HiLoGuess { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); // Creates scanner object. 
     Random numb = new Random();    // Creates an instance of the random class. 
     int guess = -1;       // Placeholder for users guess. 
     int answer = numb.nextInt(100)+1;  // Generates a random number for the game. 
     int count = 0;       // Placeholder for the guess counter. 
     int sentinel = 0;      // Placeholder for players answer as to whether they want to play again or not. 
     String newgame = "y"; 

     while (newgame.equalsIgnoreCase("y")) 
     { 
      while (guess != sentinel && guess != answer)    //Loop that ends when user enters a zero. 
      { 
       System.out.println ("Enter a number between 1-100 or 0 to quit"); 
       guess = scan.nextInt(); 
       count++; 

       if (guess < answer && guess > 0) 
       { 
        System.out.println("Your guess is too low, guess again"); 
       } 
       else if (guess > answer) 
       { 
        System.out.println ("Your guess is to high, guess again"); 
       } 

       else if (guess == answer) 
       { 
        System.out.println(); 
        System.out.println ("You guessed correctly, you win!!!"); 
        System.out.println ("It took you " + count + " guesses"); 
       } 
      } 
      System.out.print(); 
      System.out.println("Play another game: y or n?"); 
      newgame = scan.nextLine(); 
     } 
    } 
} 

答えて

-1

newgame = scan.nextLine();を置き換えます。これによって:newgame = scan.next();

そして、whileループ内で変数を初期化する必要があります。そのため、フラグはfalseにリセットされ、ランダムで新しい結果が生成されて推測されます。

public class Game 

{ 

public static void main(String[] args) 

{ 
Scanner scan = new Scanner(System.in); // Creates scanner object. 
Random numb = new Random(); // Creates an instance of the random class. 

String newgame = "y"; 

while (newgame.equalsIgnoreCase("y")) { 
    int count = 0; // Placeholder for the guess counter. 
    int guess = -1; // Placeholder for users guess. 
    int answer = numb.nextInt(100) + 1; // Generates a random number for the game. 
    int sentinel = 0; // Placeholder for players answer as to whether they want to play again or not. 

    while (guess != sentinel && guess != answer) // Loop that ends when user enters a zero. 
    { 
    System.out.println("Enter a number between 1-100 or 0 to quit"); 
    guess = scan.nextInt(); 
    count++; 

    if (guess < answer && guess > 0) { 
     System.out.println("Your guess is too low, guess again"); 
    } else if (guess > answer) { 
     System.out.println("Your guess is to high, guess again"); 
    } 

    else if (guess == answer) { 
     System.out.println(); 
     System.out.println("You guessed correctly, you win!!!"); 
     System.out.println("It took you " + count + " guesses"); 
    } 
    } 
    System.out.println(); 
    System.out.println("Play another game: y or n?"); 
    newgame = scan.next(); 
} 
} 
} 
+0

私はそれが解決策だとは思わない。理由を説明するケア? –

+0

nextLineは、スキップされた行のみを返します。次に、次のトークンを返します。 –

+0

答えを編集して、その理由を説明できますか? –

0

あなたは外側のループにこれらの初期化を配置する必要があります。そうしないと、彼らは最後の試合から値を維持し、内側のループは、それ以上実行されません

int guess = -1; 
int answer = numb.nextInt(100)+1; 
int count = 0;   

+0

変数を外側のループの内側に置き、最後のsystem.out.print()を削除します。ゲームは魅力的に機能しました。ありがとうございました。 –

0

そう(推測!=センチネル& &推測!=答えは)常にゲームがプレイされる最初の時間の後にfalseと評価され、そのため内部しばらくあなたの推測、センチネルをリセットしていない、または変数

に答えるんあなたが間にリセットを追加する必要がやりたいようにコードを取得するために :ループはOPのコメントのための最初のゲーム

while (guess != sentinel && guess != answer) //this is false after the first game because you don't reset variables 
      { ...} 

アップデート後に実行したことがありませんこのようなアウトターとインナー・ループは

  while (newgame.equalsIgnoreCase("y")) 
      { 
      guess = -1;     
      answer = numb.nextInt(100)+1; 
      count = 0; 
      while (guess != sentinel && guess != answer)    //Loop that ends when user enters a zero. 
       { ...} 
} 
+0

申し訳ありませんが、どうすれば変数をリセットできますか?私はプログラミングに慣れていないし、わからない。 –

+0

私はそれを理解してもらえません。どうもありがとうございました!!! –

+0

問題がないことを選択して受け入れることを覚えておいてください。 –

関連する問題