如何添加“再玩”?java的功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20228918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:26:07  来源:igfitidea点击:

How to add the "play again?" feature for java

java

提问by user3011235

Im making a guessing game for my class and I need some help for adding a "play again" feature at the end of the game when you've guessed the right number:

我正在为我的班级制作一个猜谜游戏,当你猜对了正确的数字时,我需要一些帮助来在游戏结束时添加“再玩”功能:

public class GuessingGame
{   
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int numtoguesses = rand.nextInt(1000) + 1;
        int counter = 0;
        int guess = -1;

        while (guess != numtoguesses)
        {
            System.out.print ("|" + numtoguesses + "|" + "Guess the right number: ");
            guess = input.nextInt();
            counter = counter + 1;

            if (guess == numtoguesses)
                System.out.println ("YOU WIN MOFO!");
            else if (guess < numtoguesses)
                System.out.println ("You're to cold!");
            else if (guess > numtoguesses)
                System.out.println ("You're to hot!");
        }

        System.out.println ("It took you " + counter + " guess(es) to get it correct");     
    }   
}

回答by Jeroen Vannevel

Just put another while loop over everything.

只需在所有内容上再放一个 while 循环。

boolean playing = true;

while(playing) {
  while(guess != numtoguesses) { // All code }

  System.out.println("Do you wish to play again? Y/N");
  String answer = input.nextLine();
  playing = answer.equalsIgnoreCase("y");
  count = 0;
  guess = -1;
}

Everything together:

一切都在一起:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Random rand = new Random();
    int numtoguesses = rand.nextInt(1000) + 1;
    int counter = 0;
    int guess = -1;
    boolean playing = true;

    while(playing) {

      while (guess != numtoguesses) {
        System.out.print ("|" + numtoguesses + "|" + "Guess the right number: ");
        guess = input.nextInt();
        counter = counter + 1;

        if (guess == numtoguesses)
            System.out.println ("YOU WIN MOFO!");
        else if (guess < numtoguesses)
            System.out.println ("You're to cold!");
        else if (guess > numtoguesses)
            System.out.println ("You're to hot!");
      }
    }

    System.out.println ("It took you " + counter + " guess(es) to get it correct"); 

    System.out.println("Do you wish to play again? Y/N");
    String answer = input.nextLine();
    playing = answer.equalsIgnoreCase("y");
    count = 0;
    guess = -1;   
    numtoguesses = rand.nextInt(1000) + 1; 
} 

You should extract this in a few methods, but I'll leave that up to you.

你应该用几种方法来提取它,但我会把它留给你。

回答by Amir Afghani

One simple approach would be to move the code you've written into a function

一种简单的方法是将您编写的代码移动到函数中

   public void play() { 
   ...
   }

and from maindo something like:

并从main做这样的事情:

   do { 
      play();
      playAgain = promptUser;
   } while(playAgain);

回答by Andrei Nicusan

There are a lot of options I can think about. The quickest:

我可以考虑很多选择。最快:

-- place all the code between lines int numtoguesses = rand.nextInt(1000) + 1;(inclusive) and end of main method inside an infinite loop
-- at the end of your current code block, add an interogation to the user, asking him whether he/she wants to play again (you can define a convention for the pressed keys); this part is placed also inside the infinite loop
-- if he/she doesn't want to, break the (outer) infinite loop

-- 将所有代码int numtoguesses = rand.nextInt(1000) + 1;放在无限循环中的行(包括)和 main 方法
的结尾 -- 在当前代码块的末尾,向用户添加询问,询问他/她是否想再玩一次(你可以为按下的键定义约定);这部分也被放置在无限循环内
——如果他/她不想,打破(外部)无限循环