如何使用 while 或 do-while 循环在我的 java 中实现 "play again" 功能?

How do I implement a "play again" feature in my java using a while or do-while loop?

我知道以前有人问过同样的问题,但我很难让这个人工作。编辑:它现在似乎只是无限期地保持 运行 。我不再收到错误,但代码将保持 运行 直到我停止它。

现在我的代码:

import java.util.Scanner;

public class Guess2
{

    public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            Scanner outScan = new Scanner (System.in);
            boolean input = true;
            while (input == true) {

            System.out.println(
                    "Gimme a number, any number. This number will be the maximum\n"
                            + " in a range of numbers. Your goal is to guess\n"
                            + " the number that I have picked at random"
                            + " within this range. Then, enter how many"
                            + " guesses you would like to receive.");
            int max = scan.nextInt();
            int meGuess = scan.nextInt();
            // Round whatever random number is generated
            double randomNumber =
                    (int) Math.round((max * Math.random()) + 1);
            randomNumber = (int) randomNumber;
            System.out.println("Alright now guess which number I picked.");
            int numGuess = 0;
            while (numGuess <= meGuess)
                {
                    int guess = scan.nextInt();
                    if (guess < randomNumber)
                        {
                            System.out.println("Too low, my friend.");
                            numGuess++;
                        } else if (guess == randomNumber)
                        {
                            numGuess++;
                            System.out.print(
                                    "Wowee Zowee! You got it right! AI "
                                            + "will absolutely never sufficiently replace "
                                            + "human intellect.\n"
                                            + "It only took you "
                                            + numGuess);
                            if (numGuess == 1)
                                {
                                    System.out.println(" try.");
                                } else
                                {
                                    System.out.println(" tries.");
                                }
                            System.out.println("Do you want to play again? Y/N");
                            String answer = outScan.nextLine();
                            input = answer.equalsIgnoreCase("y");
                            numGuess = 0;
                            meGuess = 0;
                            max = 0;
                        } else if (guess > randomNumber)
                        {
                            System.out.println("Too high, buddy.");
                            numGuess++;
                        }
                    if (numGuess > meGuess)
                        {
                            System.out
                                    .println("Sorry buddy, you used up more"
                                            + " guesses than you told me to give you."
                                            + " Maybe next time.");
                            System.out.println("Do you want to play again? Y/N");
                            String answer = outScan.nextLine();
                            input = answer.equalsIgnoreCase("y");
                            numGuess = 0;
                            meGuess = 0;
                            max = 0;
                        }

                }
            scan.close();
            outScan.close();
        }}



}

一些背景

  1. 我能够毫无问题地让这个游戏至少玩一次。出于某种原因,有时要猜的数字的正确答案是 +1 超出范围(例如,我最大 3,猜测 5,而正确答案最终以某种方式成为 4)。我也可以进行比我说的要多的猜测。这不是我的首要任务,我稍后会解决这个问题,但我希望得到帮助。我确定我只是忽略了一些东西。

    1. 最后,这只是我尝试过的一种方法,即在 main 函数的顶部放置一个 while 循环。根据 another poster 的建议,我还尝试将大部分代码放入不同的函数中,并使用 do-while 循环,但这也无济于事。也许我不明白如何做那样的事情的特殊性,但我会得到一个类似的,如果不是相同的错误。

我自己已经付出了一些努力来解决这个问题,我希望这个问题符合社区标准。我将在今天晚些时候自己完成这项工作,所以无论哪种方式,我都会继续努力,但我会很感激我能得到的任何帮助。谢谢。

几个问题:

  • 从扫描器获取整数时,您没有正确处理行尾标记。在对 scan.nextInt() 的任何调用后使用 scan.nextLine(); 处理并吞下此令牌
  • 您在 withinwhile 循环中关闭扫描仪,因此当 while 循环重复并且无法获得输入时它就死了。将该代码放在 while 循环 外部 之外。
  • 您还需要重置计数器
  • 您应该只使用 一个 基于 System.in
  • 的扫描仪
  • 您是在询问是否要在内部 while 循环中再次播放,这毫无意义。您需要从外部 while 循环

  • 中询问并获取此信息
  • 最重要的是你需要学会调试。请查看这个很棒的参考资料:How to debug small programs