私は、プレイした後に再度プレイしたいと思った場合、ユーザーに尋ねるシンプルな低ローゲームを開発しようとしています。外側の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();
}
}
}
私はそれが解決策だとは思わない。理由を説明するケア? –
nextLineは、スキップされた行のみを返します。次に、次のトークンを返します。 –
答えを編集して、その理由を説明できますか? –