jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes GUI number guessing game program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GUI number guessing game program" Watch "GUI number guessing game program" New topic
Author

GUI number guessing game program

Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Ok, so here's the issue. I can compile and run it fine, but when I load the game, it will only take one guess and will let me go no further. In other words, it asks for my guess and I enter say "45", and it says "Too high." Well, I enter another number, but then it won't go anywhere. Can you look at the code below and help me? Also, if anyone could give me any hints as to how to show the number of guesses a user has made so far, it would be greatly appreciated. Thanks!
I was wrong, it will let me move, but the number is constantly "Too High" and won't change. Any help would be appreciated.


Add the section that makes the program run...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Usually you would use a loop for something like this. Seeing as you're executing your code on the Event Dispatcher thread, that may cause some painting problems, so you could also trigger another button press from code:


Also, there are two small errors in your code:
Shouldn't that be > and < respectively, not >= and <=? Because if Guess == number, the bodies of both if-statements are executed - but the guess is right!

Also, is equal to since both Guess and number are ints.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Ok, so I changed the guess problems. Thanks for the help on that!!
The problem I seem to be having though is that the program won't recognize that I've put a different number in. (i.e. I'll put the number 45 in and the program will say "Too High", so I'll put the number 33 in and it'll again say "Too High", all the way down to number 1, which will again say "Too High". Obviously my program is not working correctly somewhere, but I cannot for the life of me figure out where.

Also, I'm not quite sure I understand what you mean with the button



What would that be used for and where?

Thanks,
Kat
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the main problem seems to be that 'number' is always 0
private int number; // application's number

put in some System.out.println's to confirm (put them in the actionListener)
then go through the constructor to find out why.
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
I think Michael has already helped with why your program isn't working, so I'm not going to comment on that.

However, I do wonder why you're calling "SwingUtilities.updateComponentTreeUI(Component)". Your program seems to work fine without it. It'll definitely slow you down if you use mysterious functions: "I don't know what it does, but it seems to be working." (Also known as "programming by coincidence," or more simply, voodoo.)

The API for that method is here, by the way. If you don't understand its description, that's a clue to not use it.
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Thank you all for your help!

I finally got the program working meaning that it actually recognizes the different numbers.

I have only one more problem and I'm hoping someone can lead me in the right direction:
I need to post how many guesses the person has attempted and I'm kind of at a loss as to how to do that.

Any insight would be great, thanks!

Kat
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
Well, you're using "private int number" to keep track of the "secret number," so you need something similar to keep track of the number of guesses.

You're using labels, e.g. "messageJLabel.setText('Too High.')" to display hints, so you can use a label to display the number of guesses so far.


p.s. Since "JLabel.setText(String)" expects a String and not an "int", you'll need to convert to display the number of guesses.
Kathleen Tillman
Greenhorn

Joined: May 09, 2009
Posts: 12
Ok, I added the Guesses = 0 and the Guesses++ to my program. The problem I'm having (at least I think) is I can't figure out where to put the JLabel so that the integer "Guesses" will actually move up. Right now, it just shows Number of Guesses = 0 when I run the program and the number never changes. Here's what I have.
Constructor portion:


Program portion:


Any help given would be appreciated! Thanks!
Kat
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
The "newGameJButton" action listener is run everytime the user clicks "New Game".

The "guessInputJTextField" action listener ("GuessHandler") is run everytime the user makes a guess (i.e. hits "Enter" with the cursor in the text field).
Vinay Berry
Greenhorn

Joined: Dec 04, 2009
Posts: 5




I changed a few things and everything works
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI number guessing game program
 
Similar Threads
Newbie, Please Help: Why can I not get the numbers to display
Applets...pie chart from user inpiut
Java Guessing Game - OutOfMemoryError
help needed with my while loop
Background color change help