2

I am writing this guessing game, which enables the user to enter a number as the maximum number.

The random generator will then pick a number between 1 and the max number entered by the user.

It will save and display the number of high guesses and low guesses.

The problem I am having is looping the program and validating.

I can not get the program to loop back after a user has entered the wrong input, i.e. not an integer.

I want it to loop back for another guess after displaying an error message.

I am using a try/catch but after the error message the program does not allow the user to enter another number to continue the game.

import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;

public class guessinggame { // class name

    public static void main(String[] args) { // main method

        String smax = JOptionPane.showInputDialog("Enter your maximum number for the Guessing Game:");
        int max = Integer.parseInt(smax);

        do {
            if (max > 10000) {
                JOptionPane.showMessageDialog(null, "Oh no! Please keep your number less than 10,000.");
                smax = JOptionPane.showInputDialog("Enter your maximum number for the Guessing Game:");
                max = Integer.parseInt(smax);
            }
        } while (max > 10000);

        int answer, guess = 0, lowcount = 0, highcount = 0, game;
        String sguess;
        Random generator = new Random();
        answer = generator.nextInt(max) + 1;

        ArrayList<String> buttonChoices = new ArrayList<>(); // list of string arrays called buttonChoices 
        buttonChoices.add("1-" + max + " Guessing Game");

        Object[] buttons = buttonChoices.toArray(); // turning the string arrays into objects called buttons

        game = JOptionPane.showOptionDialog(null, "Play or Quit?", "Guessing Game",
                JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE,
                null, buttons, buttonChoices.get(0));

        do {
            sguess = JOptionPane.showInputDialog("I am thinking of a number between 1 and " + max + ". Have a guess:");
            try {
                guess = Integer.parseInt(sguess);
            } catch (Exception nfe) {
                JOptionPane.showMessageDialog(null, "That was not a number! ");
            }

            if (guess < answer) {
                JOptionPane.showMessageDialog(null, "That is too LOW!");
                lowcount++;

            } else if (guess > answer) {
                JOptionPane.showMessageDialog(null, "That is too HIGH!");
                highcount++;
            }

        } while (guess != answer);
        JOptionPane.showMessageDialog(null, "Well Done!" + "\n---------------" + "\nThe answer was " + answer + "\nLow Guesses: " + lowcount
                + "\nHigh Guesses: " + highcount + "\n\nOverall you guessed: " + (lowcount + highcount) + " Times");

        System.exit(0);
    }
}

3 Answers 3

1

Ran it on my machine, and it loops back fine. It is broken though; guess is 0 by default, and you continue executing the code after the exception. If it happens that the random number is 0, the program will exit as it's the correct guess. Assuming something like this is happening on your machine?

You also haven't put the protecting logic around the initial "what's your max number" bit.

Sign up to request clarification or add additional context in comments.

Comments

0

Use continue; in the try catch.

the code will be like

        try {
                guess = Integer.parseInt(sguess);
            } catch (Exception nfe) {
                JOptionPane.showMessageDialog(null, "That was not a number! ");
                continue;
            }

Comments

0

While I can't see the problem you are describing, may I suggest that you put the comparison code inside the try block, so that you can be sure the guess variable will be set properly before doing any comparison:

        try {
            guess = Integer.parseInt(sguess);
            if (guess < answer) {
                JOptionPane.showMessageDialog(null, "That is too LOW!");
                lowcount++;

            } else if (guess > answer) {
                JOptionPane.showMessageDialog(null, "That is too HIGH!");
                highcount++;
            }
        } catch (Exception nfe) {
            JOptionPane.showMessageDialog(null, "That was not a number! ");
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.