程序将为第一个数字猜测生成正确的输出,但在用户输入第二个猜测后,根本没有输出。请帮忙!致谢
final int number = (int)((Math.random()*99)+1);
int counter = 0;
System.out.print("Enter a guess between 1 and 100: ");
while (keyboard.nextInt() > number) {
System.out.println("Your guess was too high. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() < number) {
System.out.println("Your guess was too low. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() == number) {
System.out.println("Congratulations! Your guess was correct!");
counter++;
System.out.println("I had chosen " + number + " as the target number.");
System.out.println("You guessed it in " + counter + " tries.");
if (counter == 1) {
System.out.println("That was lucky!");
}
if (counter >= 2 || counter <= 4) {
System.out.println("That was amazing!");
}
if (counter == 5 || counter == 6) {
System.out.println("That was good.");
}
if (counter == 7) {
System.out.println("That was OK.");
}
if (counter == 8 || counter == 9) {
System.out.println("That was not very good.");
}
if (counter >= 10) {
System.out.println("This just isn't your game.");
}
答案 0 :(得分:3)
你真的需要改变你的设计模式。
一旦你不能满足其中一个循环中的条件,你的代码将永远不会回来。
你应该只有1 while
个循环用于猜测阶段。你的代码应该是这样的
while(someCondition)
{
int num = keyboard.nextInt()
if (num > number) {
...
}
else if (num < number) {
...
}
else if (num == number) {
...
}
}
答案 1 :(得分:2)
while
循环应为if
语句。while
循环(实际上)所有代码您的代码应大致如下:
while(true) {
// prompt for input
// read input
// break from loop if input is the exit input, eg -1
// check input - essentially change your whiles to ifs
}
答案 2 :(得分:0)
此块似乎无法正常工作:
while (keyboard.nextInt() > number) {
System.out.println("Your guess was too high. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() < number) {
System.out.println("Your guess was too low. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
假设用户输入-1然后输入101.您将进入下一个循环。因此,您可能希望将其更改为:
boolean retry = true;
while ( retry ) {
counter++;
int n = keyboard.nextInt();
if( n > number ) {
System.out.println("Your guess was too high. Try again.");
}
else if( n < number ) {
System.out.println("Your guess was too low. Try again.");
}
else {
//number found
retry = false;
}
if( retry ) {
System.out.prnt("Enter a guess between 1 and 100: ");
}
}
答案 3 :(得分:0)
使用while语句时存在一些问题。
您可以在此处找到正确的代码版本:
public static void main(final String[] args) {
final Scanner keyboard = new Scanner(System.in);
final int number = (int) ((Math.random() * 99) + 1);
int counter = 0;
System.out.print("Enter a guess between 1 and 100: ");
int user_number;
do {
user_number = keyboard.nextInt();
if (user_number > number) {
System.out.println("Your guess was too high. Try again.");
System.out.print("Enter a guess between 1 and 100: ");
counter++;
} else if (user_number < number) {
System.out.println("Your guess was too low. Try again.");
System.out.print("Enter a guess between 1 and 100: ");
counter++;
}
} while (user_number != number);
System.out.println("Congratulations! Your guess was correct!");
counter++;
System.out.println("I had chosen " + number + " as the target number.");
System.out.println("You guessed it in " + counter + " tries.");
if (counter == 1) {
System.out.println("That was lucky!");
}
if ((counter >= 2) || (counter <= 4)) {
System.out.println("That was amazing!");
}
if ((counter == 5) || (counter == 6)) {
System.out.println("That was good.");
}
if (counter == 7) {
System.out.println("That was OK.");
}
if ((counter == 8) || (counter == 9)) {
System.out.println("That was not very good.");
}
if (counter >= 10) {
System.out.println("This just isn't your game.");
}
}
答案 4 :(得分:0)
while(someCondition)
{
if (keyboard.nextInt() > number) {
...
} else if(keyboard.nextInt() < number) {
...
} else {
...
}
}