1

The program will generate the correct output for the first number guess but after the user inputs the second guess, there is no output at all. Please help! THANKS

    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.");
        }
4

5 回答 5

3

你真的需要改变你的设计模式。

一旦您未能满足其中一个 while 循环中的条件,您的代码将永远不会返回。

您应该只有 1 个while循环用于猜测阶段。你的代码应该是这样的

while(someCondition)
{  
    int num = keyboard.nextInt()
    if (num  > number) {   
        ...      
    }
    else if (num  < number) {  
        ...
    }
    else if (num  == number) {
        ...
    }
}
于 2013-09-23T15:05:21.057 回答
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
}
于 2013-09-23T15:07:25.753 回答
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: ");
    } 
}
于 2013-09-23T15:06:40.790 回答
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.");
    }
}
于 2013-09-23T15:08:12.323 回答
0
while(someCondition)
{

    if (keyboard.nextInt() > number) {

        ...

    } else if(keyboard.nextInt() < number) {

        ...

    } else {

        ...

    }

}

于 2013-09-23T15:09:11.103 回答