【问题标题】:Looping this guessing game continuously不断循环这个猜谜游戏
【发布时间】:2025-11-22 20:30:01
【问题描述】:

我对 java 还很陌生,我想知道如何在用户猜对后重新设置这个游戏以询问另一个数字?

到目前为止,这是我的代码:

import java.util.Scanner;
public class Question2 {

   public static void main(String args[]) {
      Scanner keyboard = new Scanner(System.in);
         int count = 0;
         int a = 1 + (int) (Math.random() * 99);
         int guess = 0;

      System.out.println("Welcome to the Number Guessing Game");
      System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");

    while (guess != a) {
    guess = keyboard.nextInt();
     count++;
    if (guess < 0 || guess > 100){
     if(guess == -1){
        System.out.print("Thank you for playing the game!");
             break;
      }
        System.out.print("Out of bounds. Try Again: ");
        continue;
    }
    if (guess > a) {
        System.out.print("The number is lower. Try again: ");
    }
    else if (guess < a) {
        System.out.print("The number is higher. Try again: ");
    }
    else if (guess == a) {
    System.out.println("Congratulations. You guessed the number in "
    + count + " tries!");
    }  
   }

}
}

【问题讨论】:

  • 不...在答案中?我很失望:(

标签: java loops if-statement for-loop while-loop


【解决方案1】:

只需将整个代码(扫描仪初始化除外)包装在始终正确的while 循环中。这样,当一场比赛结束时,它将开始新的一场比赛。然后,当用户输入-1 时,不要使用break 执行游戏的while 循环,而只需使用System.exit(0),它会以0 的状态码结束您的程序,表示程序执行成功。

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    while (true) {
        int count = 0;
        int a = 1 + (int) (Math.random() * 99);
        int guess = 0;

        System.out.println("Welcome to the Number Guessing Game");
        System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");

        while (guess != a) {
            guess = keyboard.nextInt();
            count++;
            if (guess < 0 || guess > 100) {
                if (guess == -1) {
                    System.out.print("Thank you for playing the game!");
                    System.exit(0);
                }
                System.out.print("Out of bounds. Try Again: ");
                continue;
            }
            if (guess > a) {
                System.out.print("The number is lower. Try again: ");
            } else if (guess < a) {
                System.out.print("The number is higher. Try again: ");
            } else if (guess == a) {
                System.out.println("Congratulations. You guessed the number in "
                        + count + " tries!");
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    将您的代码包装在while (true) 中,这将永远持续运行您的代码。确保在每场比赛后更新您的随机a 和您的count。然后从那里检查猜测是否曾经是-1return。当您调用return 时,它将结束结束游戏的方法。

    Scanner keyboard = new Scanner(System.in);
    while (true){
        int count = 0;
        int a = 1 + (int) (Math.random() * 99);
        int guess = 0;
        System.out.println("Welcome to the Number Guessing Game");
        System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
    
        while (guess != a) {
            guess = keyboard.nextInt();
            count++;
            if (guess==-1){
                System.out.print("Thank you for playing the game!");
                return;
            }else if (guess < 0 || guess > 100){
                System.out.print("Out of bounds. Try Again: ");
                continue;
            }
            if (guess > a) {
                System.out.print("The number is lower. Try again: ");
            }
            else if (guess < a) {
                System.out.print("The number is higher. Try again: ");
            }
            else if (guess == a) {
                System.out.println("Congratulations. You guessed the number in "
                        + count + " tries!");
            }  
        }
    
    }
    

    【讨论】:

    • 我只需要继续循环整个程序,而不需要“你想再玩一次吗?”。如何循环播放?
    • @anthony 你不想停下来吗?你想用什么条件来停止程序?
    • 所以程序会要求用户猜一个数字,当用户猜对后它会再次要求另一个数字,依此类推。用户将输入“-1”退出程序。
    • @anthony 我已经更新了我的答案以适应你想要完成的目标。
    【解决方案3】:

    你需要:

    • 将你的结束游戏条件移动到while 条件中,像这样while(guess != -1)
    • 在循环中移动欢迎问候
    • 在游戏循环结束后移动谢谢问候
    • 当用户赢得游戏时重置 counta 以便重新开始
    • 在每次迭代时重置guess

    现在即使玩家猜到了数字,循环不会结束,游戏循环只能有意停止(输入 -1 = 当前中断条件) :

    import java.util.Scanner;
    public class Question2 {
        public static void main(String args[]) {
            Scanner keyboard = new Scanner(System.in);
            int count = 0;
            int a = 1 + (int) (Math.random() * 99);
            int guess = 0;
    
            System.out.println("Welcome to the Number Guessing Game");
            while (guess != -1) {
                System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
                guess = keyboard.nextInt();
                count++;
                if (guess < 0 || guess > 100){
                    System.out.print("Out of bounds. Try Again: ");
                    continue;
                }
                if (guess > a) {
                    System.out.print("The number is lower. Try again: ");
                }
                else if (guess < a) {
                    System.out.print("The number is higher. Try again: ");
                }
                else if (guess == a) {
                    a = 1 + (int) (Math.random() * 99);
                    System.out.println("Congratulations. You guessed the number in " + count + " tries!");
                    count = 0;
                }
                guess = 0;
            }
            System.out.print("Thank you for playing the game!");
    }
    

    代码可以重构更多,例如将功能提取到函数中以使代码更具可读性。如果变量发生变化或出现更多情况,这也会导致更容易维护。例如代码可以这样重构:

    import java.util.Scanner;
    public class Question2 {
        public static void main(String args[]) {
            Scanner keyboard = new Scanner(System.in);
            int a = 0;
            int count = 0;
            int guess = 0;
    
            startNewGame();
    
            System.out.println("Welcome to the Number Guessing Game");
    
            while (guess != -1) {
                System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
                guess = keyboard.nextInt();
                count++;
                if (guess < 0 || guess > 100){
                    System.out.print("Out of bounds. Try Again: ");
                    continue;
                }
                if (guess > a) {
                    System.out.print("The number is lower. Try again: ");
                }
                else if (guess < a) {
                    System.out.print("The number is higher. Try again: ");
                }
                else if (guess == a) {
                    System.out.println("Congratulations. You guessed the number in " + count + " tries!");
                    startNewGame();
                }
                resetGuess();
            }
            System.out.print("Thank you for playing the game!");
        }
    
        private static int generateNewA() {
            return 1 + (int) (Math.random() * 99);
        }
    
        private static void startNewGame() {
            a = generateNewA();
            count = 0;
        }
    
        private static void resetGuess() {
            guess = 0;
        }
    }
    

    另一种解决方案是使用两个嵌套循环,但 IMO 对于这种情况 loop in a loop 太多了,使得源代码变得不必要的复杂。

    【讨论】:

    • 这不会更新a,这是您每次玩游戏时生成的随机数。
    • @gonzo 然后将a也移动到while循环体中!
    • ;) 是的,可以解决它。
    • 我已经更新了我的答案。现在a 在每个循环中一遍又一遍地生成。
    • 新游戏开始时,猜测次数不会恢复为零。 :p