• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI number guessing game program

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kathleen Tillman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




I changed a few things and everything works
reply
    Bookmark Topic Watch Topic
  • New Topic