0

I’m writing a small program that is asking that user to guess a number between 1 and 100. My idea was to make several methods one with game playGame(), one that shows menu showMenu(), one for statistic. I placed the menu inside a while loop in the main method hoping that every time a game is played it will the menu and ask the user for input. Most of it works fine buy I can’t get the program flow right.

Every time I finish a game, a new game starts. I think that the problem is in the while loop inside the many method. It works fine if I change:

 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

To:

    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

But I can’t understand why. In the firs example after the game is played a new game starts without showing the menu. It jumps directly to case: 1 Thanks a lot!

You can see the program below:

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

}

} //end class GuessTheNumber

  • Welcome on this site. Please provide a [mcve] (emphasis on *minimal*) to reproduce the problem. – Thomas Weller Nov 22 '17 at 23:36
  • 3
    `selectMenu` is constant when you get inside the `while(true)` loop, so the branch selected in the `switch` case is always the same. – M. Shaw Nov 22 '17 at 23:39
  • Possibly related: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – Thomas Weller Nov 22 '17 at 23:39
  • You are not asking the user to give another choice. You simply ask once and the while loop never ends because they never get to press another button. switch case is always what is pressed at first and never asks for another input – koksalb Nov 22 '17 at 23:42
  • `while(true)` indicates a serious flaw in logic. This is inadvisable and considered poor coding style. – Taylor Nov 22 '17 at 23:52

1 Answers1

0

As this seems like a homework project, here is a quick tip for finding loop issues - simply add System.out.println("") checks inside and outside of all loop and logic conditions.

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

You will save yourself a ton of time as you'll be able to tell where your logic is breaking.

As a hint, take a look at int selectMenu = input.nextInt();. I would recommend printing the value of selectMenu using my advice above. You might be surprised to see what's assigned there.

Steven
  • 51
  • 3