如何要求用户"play again"并重新运行带有简单是或否问题的 do while 循环?



所以我添加了打印出来的yes或no语句,但它不允许用户输入。我该如何解决这个问题?我可以在do while循环中嵌套while循环吗?

import java.util.Scanner;
import java.util.Random;
public class HiLo {
public static void main(String[] args) {
Random random = new Random();
Scanner input = new Scanner(System.in);
final int MAX = 100;
int answer = random.nextInt(MAX) + 1;
int guess = -1;
int numberOfGuesses = 0;
String cont;
System.out.println("Guess a number between 1 and " + MAX + ": ");
do {
while (guess != answer) {
numberOfGuesses++;
guess = input.nextInt();
if (guess == 101) {
break;
} else if (guess > answer) {
System.out.println("Too High");
System.out.println("nGuess again ");
} else if (guess < answer) {
System.out.println("Too Low");
System.out.println("nGuess again: ");
} else {
System.out.println("Correct! The number was " + answer + ".");
System.out.println("It took you " + numberOfGuesses + " guesses.");
}
}
System.out.println("nWould you like to play again (yes/no)?");
cont = input.nextLine();
} while (cont.equals("yes");
input.close();
}
}

好吧,当我观察你的循环逻辑时,它可以作为魅力来维护和工作,你只需要在开始时将初始化步骤从外部移动到内部do-while循环。

这样,在比赛的每一次开始,一切都会奏效。

此外,在阅读用户选择的yes/no之前,您忘记了转到下一行,因为您只阅读了ints,在包含判决yes/no的行之前的最后一行

我在一个在线编译器中尝试了这段代码,它正在运行:

import java.util.Scanner;
import java.util.Random;
public class Main{
public static void main(String []args){
Random random = new Random();
Scanner input = new Scanner(System.in);
final int MAX = 100;
System.out.println("Guess a number between 1 and " + MAX + ": ");
String cont = new String();
do {
System.out.println("Let's play");
//Initializations here
int answer = random.nextInt(MAX) + 1;
int guess = -1;
int numberOfGuesses = 0;

while (guess != answer) {
numberOfGuesses++;
guess = input.nextInt();
if (guess == 101) {
break;
} else if (guess > answer) {
System.out.println("Too High");
System.out.println("nGuess again ");
} else if (guess < answer) {
System.out.println("Too Low");
System.out.println("nGuess again: ");
} else {
System.out.println("Correct! The number was " + answer + ".");
System.out.println("It took you " + numberOfGuesses + " guesses.");
}
}
System.out.println("nWould you like to play again (yes/no)?");
input.nextLine();//You have to go to the next line...
cont = input.nextLine();
} while (cont.equals("yes"));
System.out.println("nThanks for playing !");
input.close();
}
}

我会先把它组织成一个带有方法的类。这就分解了每一个逻辑,使其更容易阅读。

  1. 开始"播放"循环
  2. 在"播放"循环中,有一个"猜测"循环
  3. 将"continue"状态存储在已知常量中
  4. 只有在他们进行了最大次数的猜测后,才检查他们是否正确
  5. 用当前系统时间为Random对象设定种子

此外,我添加了一个DEBUG标志来显示答案。

更新:程序忽略甚至不询问您的"是"输入,因为您以前向它询问的是整数。您需要通过调用nextLine来清除/刷新/重置扫描仪的缓冲区。从整数切换到字符串会在缓冲区中留下一些数据。因此,您的程序将在提示您输入之前使用缓冲区中剩余的字符数据。

import java.util.Scanner;
import java.util.Random;
public class HiLo implements Runnable {
private static boolean DEBUG = true; // Disable to not show the answer...
private static final int MAX_NUMBER = 100;
private static final int MAX_GUESSES = 5;
private static final String YES = "YES";
private final Random rand;
public HiLo() {
this.rand = new Random(System.currentTimeMillis());
}
public static void main(String[] args) {
new Thread(new HiLo()).start();
}
@Override
public void run() {
Scanner scan = new Scanner(System.in);
String cont = "";
do {
play(scan);
System.out.print("nWould you like to play again (yes/no)? ");
cont = scan.nextLine();
System.out.println();
} while (cont.equalsIgnoreCase(YES));
scan.close();
}
public void play(Scanner scan) {
boolean correct = false;
int numberOfGuesses = 0;
int answer = rand.nextInt(MAX_NUMBER) + 1;
if (DEBUG) System.out.printf("The answer is: %d%n", answer);
System.out.printf("Guess a number between 1 and %d%n", MAX_NUMBER);
while (numberOfGuesses < MAX_GUESSES && !correct) {
numberOfGuesses++;
correct = makeGuess(scan, answer, numberOfGuesses);
}
if (numberOfGuesses <= MAX_GUESSES && correct) {
System.out.printf("Correct! The number was %d. It took you %d guesses.%n", answer, numberOfGuesses);
} else {
System.out.printf("Sorry, but you did not guess: %d", answer);
}
scan.nextLine(); // Reset...
}
public boolean makeGuess(Scanner scan, int answer, int currentGuesses) {
System.out.print("Guess: ");
int guess = scan.nextInt();
if (guess == answer) {
return true;
}
if (currentGuesses < MAX_GUESSES) {
if (guess > answer) {
System.out.print("Too High, ");
} else {
System.out.print("Too Low, ");
}
}
return false;
}
}

样本输出

The answer is: 64
Guess a number between 1 and 100
Guess: 50
Too Low, Guess: 75
Too High, Guess: 60
Too Low, Guess: 65
Too High, Guess: 64
Correct! The number was 64. It took you 5 guesses.
Would you like to play again (yes/no)? yes
The answer is: 88
Guess a number between 1 and 100
Guess: 50
Too Low, Guess: 90
Too High, Guess: 80
Too Low, Guess: 85
Too Low, Guess: 87
Sorry, but you did not guess: 88
Would you like to play again (yes/no)? no

最新更新