[java] Help: Guess my Number GUI

Started by
5 comments, last by Graphain 17 years, 5 months ago
I wanted to try using GUIs, and I decided to try to create a Guess My Number game using GUIs. However, when the program asks "Do you wish to play again?" and the user clicks "No", the program makes the user play again anyway. I can't figure out why. I've occasionally had the program successfully allow a quit upon clicking "No", but I can't figure out what was different about what I did. Anyway, here is the code. Can anyone help me to see what's causing this problem?


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

public class GuessMyNumber
{
	public static void main(String[] args)
	{
		Random generator = new Random();
		String guessStr;
		int currentGuess = -1;
		int guessCount = 0;
		
		int repeat = JOptionPane.YES_OPTION;
		int overallRepeat = JOptionPane.YES_OPTION;
		
		do 
		{
		
			int correctNum = generator.nextInt(100) + 1;
			repeat = JOptionPane.YES_OPTION;
		
			do 
			{
			
			JOptionPane.showMessageDialog(null, correctNum);
			guessCount++;
			guessStr = JOptionPane.showInputDialog (null, "I am thinking of a number between 1 and 100.\nWhat is your guess?");
			currentGuess = Integer.parseInt(guessStr);
		
	
			
			if (currentGuess > correctNum)
				repeat = JOptionPane.showConfirmDialog(null, "You guessed too high. Guess again?");
			else if (currentGuess < correctNum)
				repeat = JOptionPane.showConfirmDialog(null, "You guessed too low. Guess again?");
			else if (currentGuess == correctNum)
				repeat = JOptionPane.CANCEL_OPTION;
			
			}
			while (repeat == JOptionPane.YES_OPTION);
		
		
		
		
			if (currentGuess == correctNum)
			{
				JOptionPane.showMessageDialog(null, "You guessed it! It took you " + guessCount + " tries.");
				
				
			}
			guessCount = 0;
			overallRepeat = JOptionPane.showConfirmDialog(null, "Play a new game?");
		
		
		}
		while (overallRepeat == JOptionPane.YES_OPTION);
		
		
		JOptionPane.showMessageDialog(null, "Thank you for playing.");
	}
}


Thanks for any help.
Advertisement
Not the way I would do it but I can't see anything wrong with it at all. If you get stuck in a bind you could do a conditional statement check and a break statement but it doesn't really hold with your code. Just maybe the code isn't waiting for the dialog response but then again your early loop is working. Sorry I couldn't be more help.
Set breakpoint, run in debugger. See what's really going on. That's all I can suggest.
I've tried to run the debugger in Jcreator many times, and I've still never gotten it.
I ran that code from the OP "as is" under Java 1.5.0 in eclipse, and it behaves absolutely as expected. "No" and "Cancel" of "Play a new game" let the program quit after its "Thank you".

Perhaps your loop isn't inside the program itself but your test environment? Are you sure you're running exactly that version?
Yes, I'm running exactly that version. Perhaps I'll try another environment. I've tried compiling in both JCreator and command prompt's javac, and running them in each. Same effect.
I'll try it on the school computers tomorrow I suppose.
Put in some print statements printing out your values at key locations (especially at the end of your do statement for the overallRepeat variable). If it prints out the correct value (i.e. System.out.println(overallRepeat == JOptionPane.YES_OPTION); prints 0 (I can't remember if it auto-converts ints to Strings, if not type ""+overallRepeat...)) and doesn't end then you can presume there is an error in your runtime environment.

This topic is closed to new replies.

Advertisement