【问题标题】:Java High-Low Guessing Game Looping ErrorsJava 高低猜测游戏循环错误
【发布时间】:2018-07-08 20:20:30
【问题描述】:

我刚刚开始第一次学习编程,我正在通过 Java 开始学习。我正在做一个使用循环和条件来编写猜谜游戏的常见编码练习。我的程序需要执行以下操作:

  1. 在 1 到 100 之间选择一个随机数
  2. 反复提示用户猜数字
  3. 向用户报告他或她是正确的或猜测很高 或每次猜测后低
  4. 为用户提供中途退出的选项
  5. 计算游戏中的猜测次数,并在正确猜测时报告数字
  6. 询问用户是否想在游戏成功后再次玩游戏

到目前为止,我对循环语法有点不放心,我的程序需要一些帮助,因为有很多问题我不知道如何解决。有人愿意帮我一把吗?请原谅许多可能明显的错误。


导入 java.util.Scanner;

导入 java.util.Random;

公开课猜

{

public static void main (String[] args)

{

    final int MAX = 100;
    int answer, guess = 0, count = 0;
    String another = "y";

    Random generator = new Random();
    answer = generator.nextInt(MAX) + 1;

    Scanner scan = new Scanner(System.in);

    System.out.println("I'm thinking of a number between 1 and " + MAX
                        + ". Guess what it is: ");
    guess = scan.nextInt();

    while (another.equalsIgnoreCase("y"))
    {         
        while (guess != answer)
        {

            while (guess > MAX || guess < 1)
            {
                System.out.println("Invalid input. Please re-enter a number"
                + " between 1 and " + MAX + ":");
                guess = scan.nextInt();
            }

            if (guess == answer)
            {      
            count++;
            System.out.println("You guessed correctly!");
            }
            else if (guess > answer)
            {
                count++;
                System.out.println("You guessed too high. Guess again? Y/N:");
                another = scan.nextLine();
            }
            else if (guess < answer)
            {
                count++;
                System.out.println("You guessed too low. Guess again? Y/N:");
                another = scan.nextLine();
            }
        }
    }

    System.out.println("It took you " + count + "guess(es) to win.");
    System.out.println("Do you wish to play again? Y/N:?");
    another = scan.nextLine();
    count = 0;
    answer  = generator.nextInt(MAX) + 1;
}

}

【问题讨论】:

    标签: java


    【解决方案1】:

    一个问题是您不能让用户在游戏中途退出,因为即使用户猜到了 1 到 100 范围内的数字并且没有得到正确答案,他或她对 Guess again: Y/N: 的回答不会被检查,因为它所在的当前循环仅将guessanswer 进行比较,而不是another。因此,在这种情况下,您最终会陷入无限循环,因为如果用户在答案为 50 时猜到了 57,那么您将不断提示用户是否要再次猜测。

    我的建议是删除第二个 while 循环

    while (guess != answer)
    {
        //other stuff
    }
    

    并将该循环内的代码放入外部while循环

    while(another.equalsIgnoreCase("y")){
        //other stuff
    }
    

    如果您希望用户能够再次玩游戏,我建议您将之前的这段 sn-p 代码放入 if 语句中,检查用户是否猜对了,

    if (guess == answer)
    {      
        count++;
        System.out.println("You guessed correctly!");
        System.out.println("It took you " + count + "guess(es) to win.");
        System.out.println("Do you wish to play again? Y/N:?");
        another = scan.nextLine();
        count = 0;
        answer  = generator.nextInt(MAX) + 1;
    }
    

    这样,如果用户赢得游戏,他们选择再次玩的选择将在 while 循环中检查。我建议的最后一件事是移动这条线

    guess = scan.nextInt();
    

    在检查another的while循环中,如果用户想再次玩游戏,游戏会提示用户猜测。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      相关资源
      最近更新 更多