猜测数字的程序

Bru*_*uno 0 java while-loop

大家好我正在写一个与用户玩猜谜游戏的程序.你需要考虑一个数字,程序会猜到它.这是我的代码:

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Guess a number between 1 do 100 and i'll guess it");
        int min = 0;
        int max = 100;
        int guess = (max-min)/2 + min;
        boolean end = false;
        while(!end){

            System.out.println("zgaduje " + guess);
            String userInput = scan.next();
            if(userInput.equalsIgnoreCase("too much")){
                max = guess;
            }
            else if(userInput.equalsIgnoreCase("too small")){
                min=guess;
            }
            else if(userInput.equalsIgnoreCase("correct")){
                end = true;
            }
             guess = (max-min)/2 + min;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以程序猜测一个数字,然后根据用户输入(太小或太多)再次猜测.它没有按预期工作,它只显示第一次猜测.你知道这里可能有什么问题吗?

Kun*_*Lun 7

你用scan.next();.对于输入too much,仅返回too.

要阅读整行,您需要使用 scan.nextLine();