[英]How do I ask the user to "play again" and rerun the do while loop with a simple yes or no question?
所以我添加了 yes or 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
之前,您還忘記了 go 到下一行,因為您只閱讀int
s,最后一行位於包含判決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();
}
}
我會先用方法將其組織成 class。 這分解了每一段邏輯並使其更易於閱讀。
Random
object 播種另外,我添加了一個DEBUG
標志來顯示答案。
更新:該程序忽略或什至不詢問您的“是”輸入,因為您之前要求它輸入整數。 您需要通過調用nextLine
來清除/刷新/重置掃描儀的緩沖區。 從 integer 切換到字符串會在緩沖區中留下一些數據。 因此,您的程序將在提示您輸入之前使用緩沖區中剩余的字符數據。
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
所以我添加了 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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.