这是一个游戏,它从1到100生成一个随机数,并提示用户猜测该数字。玩完一个游戏(用户猜对了数字),游戏会让你再玩一次。它应该在每次游戏后询问,直到用户拒绝。
在我的代码中,第一个完成的游戏将问题返回给用户,但之后的游戏不会询问,而是在没有提示的情况下玩游戏。我在while循环中的语句位置是否不正确?
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
playGame(reader);
System.out.println("Do you want to play again? ");
String userInput = reader.next();
int minGuess = 0;
while(userInput.indexOf("y")!=-1 || userInput.indexOf("Y")!=-1) {
guessCount = playGame(reader);
game++;
System.out.println("Do you want to play again?"); //not sure about this placement
}
overallStatistics(game, guessCount, minGuess);
}发布于 2019-11-28 12:45:39
您应该在每次迭代的末尾阅读用户答案:
while(userInput.indexOf("y")!=-1 || userInput.indexOf("Y")!=-1) {
guessCount = playGame(reader);
game++;
System.out.println("Do you want to play again?");
userInput = reader.next(); // read user answer
}https://stackoverflow.com/questions/59081827
复制相似问题