java - 一旦你猜对了数字,如何让这个猜数字游戏重新开始?

标签 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.");
            }
        }
    }
}

最佳答案

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

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

}

}

关于java - 一旦你猜对了数字,如何让这个猜数字游戏重新开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348175/

相关文章:

java - 我将如何创建 JFreeChart 散点图最佳拟合线

java - JTextField。查找并突出显示 JTextArea 中的单词

java - Eclipse 从 CMD 提示符找到 Java,但不能从桌面单击找到 Java

c++ - Eclipse 上的 Arduino

java - 如何设置正确的 Eclipse 项目结构?

C++ 链接器错误 ("Linker command failed with exit code 1")

java - Maven:从一个较大项目的包中创建一个 jar

java - 如何处理org.xml.sax.SAXParseException?

java - 为什么 "activator start"会因 "Bad application path"而失败?

java - apache poi - 找到条件后继续循环