This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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
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.
Joined: May 09, 2009
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!
Joined: Jun 15, 2009
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.
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.
Any help given would be appreciated! Thanks!
Joined: Jun 15, 2009
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).