-5

这可能是一个显而易见的答案,但明天就要到期了,我完全被难住了。除了最后的 if 语句外,一切正常。似乎 continueGame 循环正在无限循环 if 语句。如果有人可以帮助我解决这个问题,请。

import java.util.Random;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class HighLOw {
    public static void main(String[] args) throws IOException {

        String input = null;
        int number = 0;
        int tries = 0;
        boolean highorlow = false;
        boolean continueGame = false;
        boolean validInput = false;
        Random random = new Random();
        int randomNumber = (random.nextInt(101));

        System.out.println("Welcome to the High/Low game.");
        System.out.println("Guess a number between 0 and 100.");
        System.out.println(
                "In order to print a random number, type your number in the console and press enter.");
        System.out.println("Keep going until you get the right number.");
        System.out.println("Have fun.");
        System.out.println(randomNumber);

        while (continueGame == false) { 

            while (validInput == false) {
                try {
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(System.in)); // BufferedReader variable where the user will input their answer

                    input = bufferedReader.readLine();
                    number = Integer.parseInt(input);
                    validInput = true;

                } catch (NumberFormatException ex) { 
                    System.out.println("Invalid input, please enter a number.");
                    validInput = false;
                }

                if (number < 0 || number > 100) {
                    System.out.println(
                            "Error, you must enter a number that is between 0 and 100");
                    validInput = false;
                }
            }

            if (number < randomNumber) {
                System.out.println(
                        number + " is too low. Please try a higher number");
                tries++;
                highorlow = false;
            } else if (number > randomNumber) {
                System.out.println(
                        number + " is too high. Please try a lower number");
                tries++;
                highorlow = false;
            } else if (number == randomNumber) {
                System.out.println(
                        "Congratulations, you guessed the right number. Now go outside.");
                tries++;
                System.out.println("It took you " + tries + (" tries."));
                System.out.println("Game Ended");
                continueGame = true;
            }
        }
    }
}
4

2 回答 2

4

几点提示:当您第一次输入有效数字时,validInput将变为true。然后,代码继续比较numberrandomNumber然后再次跳到continueGame循环的开头。现在的价值是validInput多少,这对validInput循环有什么影响?是否会number获得新的价值?

你也不需要做while (validInput == false)..只是while (!validInput)等价..

于 2012-10-03T20:23:26.977 回答
0

请记住,if/else语句是从上到下评估的。一旦if找到匹配项,它的主体就会被执行并退出。

牢记这一点,这意味着对if/else结构的给定评估只有一个case执行主体的位置。

看看你的if条款,把它们想象成一条数字线(或者更好地画一条数字线,上面有一些例子numberrandomNumber可能是什么),看看答案是否会突然出现在你身上。

于 2012-10-03T20:46:56.007 回答