该Java程序应该要求用户输入最大值,然后生成1到该最大值之间的随机数。然后用户猜测它的值是什么,直到它们正确为止。当用户不正确时,程序会告诉他们它们太高或太低并且增加了猜测次数。如果用户是正确的,程序会显示猜测的总数,并询问用户是否想再次播放。
我一直存在的问题是,有时候,当我输入一个猜测时,程序会跳过来告诉我,我太高,太低或者说不对,只是问我是否想再玩一次。我无法弄清楚为什么会这样,而且似乎是随机发生的。
import java.util.Scanner;
public class GuessNumber
{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in); //create scanner object
int maxNumber = 0; //declare variables
int userGuess = 0;
int totalGuesses = 0;
int secretNumber = 0;
String replay = "";
System.out.println("Welcome to the Secret Number Generator!");
do
{
System.out.println("Please input a maximum value for the secret number and press enter."); //prompt user to input a maximum value
maxNumber = in.nextInt();
secretNumber = (int) ((Math.random() * maxNumber) + 1); //generate the secret number
System.out.println("A new secret number has been chosen!"
+ "\nWhat do you think it is? Input your guess and press enter."); //prompt user to input a guess
userGuess = in.nextInt();
while (userGuess < secretNumber) //while user's guess is less than the secret number
{
System.out.println("Sorry, that is too low! Please try again."); //prompt user to input another guess; increment total number of guesses by 1
userGuess = in.nextInt();
totalGuesses ++;
}
while (userGuess > secretNumber) //while user's guess is greater than the secret number
{
System.out.println("Sorry, that is too high! Please try again."); //prompt user to input another guess; increment total number of guesses by 1
userGuess = in.nextInt();
totalGuesses ++;
}
if (userGuess == secretNumber) //if user guesses correctly
{
System.out.println("Nice job! Your guess, " + secretNumber + " is correct!" //congratulate and display total number of guesses
+ "\nTotal number of guesses: " + totalGuesses);
}
System.out.println("Would you like to play again? Please input \"Yes\" or \"No\" and press enter."); //ask user if they would like to play again
replay = in.next();
while (!replay.equalsIgnoreCase("Yes") && !replay.equalsIgnoreCase("No") //in case of invalid input
&& !replay.equalsIgnoreCase("\"Yes\"") && !replay.equalsIgnoreCase("\"No\""))
{
System.out.println("Sorry, that is an invalid input. Please input \"Yes\" or \"No\" and press enter.");
replay = in.next();
}
}
while(replay.equalsIgnoreCase("Yes") || replay.equalsIgnoreCase("\"Yes\"")); //replay while user says yes
if (replay.equalsIgnoreCase("No") || replay.equalsIgnoreCase("\"No\"")) //if user says no
{
System.out.print("Thanks for playing!");
}
} //end of main method
} //end of class GuessNumber
答案 0 :(得分:0)
不要将while
循环用于单个评估条件。用这样的东西(伪代码)替换你的三个内部while循环:
int guess = readInteger()
while guess != number
if guess < number print "too low"
if guess > number print "too high"
assert guess == number
print "you guessed correctly; good job!"
然后通过游戏循环运行,以保持游戏的进行。
答案 1 :(得分:0)
你有多个while循环没有意义。如果用户输入更大的数字然后输入小数字,它将跳过它。
你可以将它们全部收缩到其中一个
while (userGuess != secretNumber){
if(userGuess > secretNumber){
//too high
} else if(userGuess < secretNumber){
//too low
} else{
//nice got it
}
}
//do you want to play again
...
答案 2 :(得分:0)
看起来你遇到了一些循环问题,就像其他人建议的那样,当一个简单的选择结构如“if else”或“select case”工作时,请避免这么多不必要的嵌套循环。应该这样节省你一些大惊小怪。
为了让您的应用程序更可预测地运行,我建议将“游戏”逻辑放入方法中。这是一个运行的例子。希望它可以帮到你一些!
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String replay = "";
do {
runGame(); //run method to guess number
System.out.println("Play again? Enter Yes or No");
replay = in.nextLine().toLowerCase();
}
while(!replay.equals("no"));
}
private static void runGame() {
Scanner in = new Scanner(System.in); //create scanner object
int maxNumber = 0; //declare variables
int userGuess = 0;
int totalGuesses = 0;
int secretNumber = 0;
System.out.println("Welcome to the Secret Number Generator!");
System.out.println("Please input a maximum value for the secret number and press enter."); //prompt user to input a maximum value
maxNumber = in.nextInt();
secretNumber = (int) ((Math.random() * maxNumber) + 1); //generate the secret number
System.out.println("A new secret number has been chosen!" + "\nWhat do you think it is? Input your guess and press enter."); //prompt user to input a guess
userGuess = in.nextInt();
while (userGuess != secretNumber) {
if (userGuess < secretNumber) {
System.out.println("Sorry, that is too low! Please try again."); //prompt user to input another guess; increment total number of guesses by 1
} else if (userGuess > secretNumber) {
System.out.println("Sorry, that is too High! Please try again."); //prompt user to input another guess; increment total number of guesses by 1
}
else {
System.out.println("Sorry, but that's not right. Try again...");
}
totalGuesses++;
userGuess = in.nextInt();
}
System.out.println("You guessed it! After " + totalGuesses + " tries.");
}
}