小编Bru*_*uno的帖子

猜测数字的程序

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

 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)

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

java while-loop

0
推荐指数
1
解决办法
80
查看次数

标签 统计

java ×1

while-loop ×1