简体   繁体   English

如何使用 while 或 do-while 循环在我的 java 中实现“再次播放”功能?

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

I know this same thing has been asked before but I am having a lot of trouble getting this guy to work.我知道之前也有人问过同样的问题,但我在让这个人工作时遇到了很多麻烦。 EDIT: it now seems to just keep running indefinitely.编辑:它现在似乎只是无限期地运行。 I no longer get an error but the code will just keep running until I stop it.我不再收到错误,但代码会一直运行直到我停止它。

And now for my code:现在对于我的代码:

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();
        }}



}

Some background一些背景

  1. I am able to get this game to play at least once through with no problem.我能够让这个游戏至少玩一次,没有问题。 For some reason, sometimes the correct answer for the number to be guessed is +1 out of range (for example, I do max 3, guesses 5, and the correct answer ends up being 4 somehow).出于某种原因,有时要猜测的数字的正确答案是 +1 超出范围(例如,我最多猜 3 次,猜 5 次,但不知何故,正确答案最终是 4)。 I am also allowed more guesses than I said I wanted.我也被允许进行比我说我想要的更多的猜测。 This is not my priority and I will fix that later, but I would like help with that.这不是我的优先事项,我稍后会解决这个问题,但我希望得到帮助。 I'm sure I'm just overlooking something.我确定我只是忽略了一些东西。

    1. Lastly, this is only one way I have tried to do this, by putting a while loop at the top of the main function.最后,这只是我尝试执行此操作的一种方法,即在 main 函数的顶部放置一个 while 循环。 I have also tried to put the bulk of the code into a different function, as per the recommendation of another poster , and use a do-while loop, but that was also to no avail.根据另一位海报的推荐,我还尝试将大部分代码放入不同的函数中,并使用 do-while 循环,但这也无济于事。 Maybe I did not understand the specificities of how to do something like that, but I would get a similar, if not the same error.也许我不明白如何做这样的事情的特殊性,但我会得到类似的错误,如果不是同样的错误。

I have put a bit of effort into resolving this problem myself and I hope this question meets community standards.我自己花了一些精力来解决这个问题,我希望这个问题符合社区标准。 I will be finishing this up myself later today so either way I'll be working on it but I would appreciate any help I could get.我将在今天晚些时候自己完成这项工作,所以无论哪种方式我都会努力,但我会很感激我能得到的任何帮助。 Thank you.谢谢你。

Several problems:几个问题:

  • You're not handling the end of line token properly when getting ints from the Scanner.从扫描仪获取整数时,您没有正确处理行尾标记。 Follow any call to scan.nextInt() with scan.nextLine();使用scan.nextLine();跟踪对scan.nextInt()任何调用scan.nextLine(); to handle and swallow this token处理和吞下这个令牌
  • Your closing your Scanner within your while loop, so it is dead when the while loop repeats and no input is obtainable.while 循环中关闭了 Scanner,因此当 while 循环重复并且无法获得输入时,它就死了。 Put that code to close outside the while loop.将该代码关闭在 while 循环之外
  • You also need to reset your counter您还需要重置您的计数器
  • You should only use one Scanner based on System.in您应该只使用一个基于System.in扫描仪
  • You're asking if you want to play again within the inner while loop which makes no sense.你问你是否想在内部 while 循环中再次播放,这是没有意义的。 You need ask and get this information from within the outer while loop您需要从外部 while 循环中询问并获取此信息

  • Most important is that you need to learn how to debug.最重要的是你需要学习如何调试。 Please check out this great reference:How to debug small programs请查看这个很好的参考:如何调试小程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM