通过调用方法打破循环

时间:2017-11-22 23:35:00

标签: java

我正在编写一个小程序,要求用户猜测1到100之间的数字。我的想法是用游戏playGame()创建一个方法,一个显示菜单showMenu(),一个用于统计。我把菜单放在main方法的while循环中,希望每次玩游戏时都会菜单并要求用户输入。大多数工作正常,我不能让程序流程正确。

每次我完成游戏,都会开始新游戏。我认为问题出在许多方法中的while循环中。如果我改变它可以正常工作:

 public static void showMenu() {
    Scanner input = new Scanner(System.in);
    System.out.println("1. Play a game.");
    System.out.println("2 Show statistics.");
    System.out.println("3. Exit.\n");
    System.out.println("Make a choise: ");
    int selectMenu = input.nextInt();

    while (true) {
        switch (selectMenu) {
            case 1:
                playGame();
                break;
            case 2:
                statistics();
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("Enter valid number:");
        }
    }
} //end showMenu

要:

    public static void showMenu() {
    Scanner input = new Scanner(System.in);

    while (true) {
        System.out.println("1. Play a game.");
        System.out.println("2 Show statistics.");
        System.out.println("3. Exit.\n");
        System.out.println("Make a choise: ");
        int selectMenu = input.nextInt();
        switch (selectMenu) {
            case 1:
                playGame();
                break;
            case 2:
                statistics();
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("Enter valid number:");
        }
    }
} //end showMenu

但我无法理解为什么。在比赛结束后的第一个例子中,新游戏开始时没有显示菜单。它直接跳到案例:1 非常感谢!

您可以看到以下程序:

public class GuessTheNumber {
private static int gameCount;
private static int guessCount;
private static int highestNumber;
private static int lowestNumber;

public static void main(String[] args) {
    while (true) {
        showMenu();  
    }
} // end main

public static void playGame() {

    int secretNumber, guess;
    Scanner input = new Scanner(System.in);
    SecureRandom rand = new SecureRandom();



    secretNumber = rand.nextInt(100) + 1; //makes random number between 1 and 100
    System.out.println(secretNumber);

    System.out.println("Guess the secret mumber which is between 1 and 100.");
    System.out.print("Make your guess: ");
    guess = input.nextInt();
    guessCount++;
    highestNumber = guess;
    lowestNumber = guess;

    while (guess != secretNumber) {

        // high or low logic
        if (guess > secretNumber) {
            System.out.println("The number is too high.");
        }
        else {
            if (guess < secretNumber) {
                System.out.println("The number is too low.");
            }
        }

        System.out.print("Make a new guess: ");
        guess = input.nextInt();
        guessCount++;

        //get highest and lowest number
        if (guess > highestNumber) {
            highestNumber = guess;
        }
        if (guess < lowestNumber) {
            lowestNumber = guess;
        }

    } //end while
    System.out.printf("Very good the right number was: %d%n", guess);

} //end playGame()

public static void showMenu() {
    Scanner input = new Scanner(System.in);
    System.out.println("1. Play a game.");
    System.out.println("2 Show statistics.");
    System.out.println("3. Exit.\n");
    System.out.println("Make a choise: ");
    int selectMenu = input.nextInt();

    while (true) {
        switch (selectMenu) {
            case 1:
                playGame();
                break;
            case 2:
                statistics();
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("Enter valid number:");
        }
    }
} //end showMenu

public static void statistics() {
    System.out.println("Games played: " + gameCount);
    System.out.println("Total number of guesses: " + guessCount);
    System.out.println("The highest number: " + highestNumber);
    System.out.println("The lowest number: " + lowestNumber);

}

} //结束类GuessTheNumber

1 个答案:

答案 0 :(得分:0)

由于这似乎是一个家庭作业项目,这里有一个查找循环问题的快速提示 - 只需在所有循环和逻辑条件的内部和外部添加System.out.println(&#34;&#34;)检查。

EG)。 System.out.println("Before While True); System.out.println("Inside Case 1"); System.out.println("Inside Case 2");

你将节省大量的时间,因为你能够分辨出你的逻辑在哪里。

作为提示,请看一下int selectMenu = input.nextInt();.我建议使用上面的建议打印selectMenu的值。您可能会惊讶地发现在那里分配了什么。