一旦你猜对了这个号码,怎么让这个猜数游戏重新启动?

时间:2014-12-07 21:41:58

标签: java eclipse

我无法弄清楚如何让游戏重启。我想我需要把它放在System.out.println的地方(“好的,让我们再玩一次。”); 我需要在那里重新启动它?我错过了什么?

import java.util.Random;
import java.util.Scanner;

public class GuessMyNumber {

    public static void main(String[] args) {

        Random Bob = new Random();
        int NumToGuess = Bob.nextInt(20);
        int NumOfTries = 0;
        Scanner Bill = new Scanner(System.in);
        int guess;
        boolean win = false;

        while (win == false) {

            System.out.println("I'm thinking of a number between 1 and 20, can you guess it?");
            guess = Bill.nextInt();
            NumOfTries++;

            if (guess == NumToGuess) {
                win = true;
            }
            else if (guess < NumToGuess) {
                System.out.println("Your guess is too low");
            }
            else if (guess > NumToGuess) {
                System.out.println("Your guess is too high");
            }
        }

        System.out.println("Congratulations!");
        System.out.println("The number was " + NumToGuess);
        System.out.println("It took you " + NumOfTries + " tries"); 

        while (win == true) {
            String YesOrNo;
            YesOrNo = Bill.nextLine();
            System.out.println("Would you like to play again? " + YesOrNo);
            YesOrNo = Bill.nextLine();

            if (YesOrNo.equalsIgnoreCase("yes"))
            {
                System.out.println("Ok, let's play again.");
            }
            else
            {
                System.out.println("Ok, maybe next time.");
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您也可以尝试这样的事情:

public class MainClass {

public static void main(String[] args) {
    Random bob = new Random();
    Scanner bill = new Scanner(System.in);

    String yesOrNo = "";
    do {

        int numToGuess = bob.nextInt(20);
        int numOfTries = 0;
        int guess = 0;
        boolean win = false;

        while (win == false) {

            System.out
                    .println("I'm thinking of a number between 1 and 20, can you guess it?");
            guess = Bill.nextInt();
            numOfTries++;

            if (guess == numToGuess) {
                win = true;
            } else if (guess < numToGuess) {
                System.out.println("Your guess is too low");
            } else if (guess > numToGuess) {
                System.out.println("Your guess is too high");
            }
        }

        System.out.println("Congratulations!");
        System.out.println("The number was " + NumToGuess);
        System.out.println("It took you " + NumOfTries + " tries");

        yesOrNo = bill.nextLine();
        System.out.println("Would you like to play again? " + yesOrNo);
        yesOrNo = bill.nextLine();

    } while (yesOrNo.equalsIgnoreCase("yes"));

    System.out.println("Ok, maybe next time.");
    System.exit(0);

}

}

答案 1 :(得分:0)

你的第二个循环就是问题。您可能复制了第一个循环。在第二个循环中,您设置了YesOrNo而不是win。所以它会永远循环。

   while (win == true) {          //You test win
        String YesOrNo;
        YesOrNo = Bill.nextLine() //You set YesOrNo
        ...
   }                              //You never set win to anything new so it loops forever 

解决重启问题的典型方法是使用一个循环而不是两个循环,让用户在想要退出时输入-1。这样,游戏就会一直持续输赢。

顺便说一句,我们在java中使用camelCase,因此YesOrNo应该是yesOrNo,但这只是一个样式问题。

答案 2 :(得分:0)

我会将整个方法体包装到while循环中,以检查标志(例如boolean continueGame)是否为true。我的意思是这样的:

import java.util.Random;
import java.util.Scanner;

public class GuessMyNumber {
    public static void main(String[] args) {
        Random Bob = new Random();
        Scanner Bill = new Scanner(System.in);
        int guess;
        boolean continueGame = true; // the flag (boolean) that allows us to check if the user wants to continue the game or not

        while(continueGame) {
            boolean win = false;
            int NumToGuess = Bob.nextInt(20); // moved into the while loop because they need to be initialized every time you start the game
            int NumOfTries = 0;

            while (win == false) {

                System.out.println("I'm thinking of a number between 1 and 20, can you guess it?");
                guess = Bill.nextInt();
                NumOfTries++;

                if (guess == NumToGuess) {
                    win = true;
                }
                else if (guess < NumToGuess) {
                    System.out.println("Your guess is too low");
                }
                else if (guess > NumToGuess) {
                    System.out.println("Your guess is too high");
                }
            }

            System.out.println("Congratulations!");
            System.out.println("The number was " + NumToGuess);
            System.out.println("It took you " + NumOfTries + " tries"); 

            // As CandiedOrange already noted, you need to remove the while loop which was here.
            String YesOrNo;
            YesOrNo = Bill.nextLine();
            System.out.println("Would you like to play again? " + YesOrNo);
            YesOrNo = Bill.nextLine();

            if (YesOrNo.equalsIgnoreCase("yes"))
            {
                System.out.println("Ok, let's play again.");
                continueGame = true;
            }
            else
            {
                System.out.println("Ok, maybe next time.");
                continueGame = false;
            }
        }
    }
}

就是这样。如果您对此答案有疑问,请随时提出。

答案 3 :(得分:0)

您可以使用带标签的BREAK声明。 或者你需要重新运行程序,因为你的while循环结束了,你就出局了。