Search code examples
javafor-loopcounterresetjoptionpane

How to reset the counter of a for loop in java


So I am trying to create a program for my class in high school to be able to generate a random number and have a user keep guessing until they get it right.

So far I have everything going perfectly, except when I prompt them to play again, the counter that counts how many times they have guessed doesn't reset after they chose to play again. I don't know what to do for it, and searching for a solution, it was too complicated for me to understand or use.

Take note that this is only my second month of programming, so the code might seem amateur and I might seem terrible at programming. I just need a solution and perhaps an explanation.

import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.UIManager;
import java.awt.*;

public class RandomNumberGuesser{
    public static void main(String[] args){
        UIManager m1=new UIManager();
        Color g = Color.gray;
        Color lg = g.brighter();
        m1.put("OptionPane.background", lg);
        m1.put("Panel.background", lg);

        for(x = 1; true; x++){
        Random random = new Random();
        int randomNumber = random.nextInt(100);

        JOptionPane.showMessageDialog(null,
            "This program will generate a random number from 0 to 100 which you have to guess.",
            "Number Guesser",
            JOptionPane.INFORMATION_MESSAGE);
            String guess = JOptionPane.showInputDialog(null,
                "Guess a number.",
                "Guess",
                JOptionPane.QUESTION_MESSAGE);
                if(guess == null){
                    System.out.println("The user has terminated the program");
                    System.exit(0);
                }
            int guess1 = Integer.parseInt(guess);

            if(guess1 > 100 || guess1 < 0)
                JOptionPane.showMessageDialog(null,
                    "Guess is out of range!\nPlease enter valid input.",
                    "Invalid Input",
                    JOptionPane.WARNING_MESSAGE);

            else if(randomNumber > guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too low.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else if(randomNumber < guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too high.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else if(randomNumber == guess1){
                JOptionPane.showMessageDialog(null,
                    "You guessed the number right!\nIt took you "+x+" attempt(s) to guess it.",
                    "Congratulations!",
                    JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.showConfirmDialog(null, "Want to play again?", "Play again?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    System.exit(0);
                }

            }

        }
    }
}

Solution

  • if (JOptionPane.showConfirmDialog(
                       null, 
                       "Want to play again?", 
                       "Play again?",
                       JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
        System.exit(0);
    } else {
        x = 1;
    }
    

    Just reset the counter to its initial value.

    Note that you should avoid creating a new Random object every iteration. Create it once outside your loop and just call random.nextInt(100) every time.