This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Loop Issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop Issues" Watch "Loop Issues" New topic
Author

Loop Issues

David Kanton
Greenhorn

Joined: Apr 23, 2012
Posts: 2
I am trying to develop a number guessing game using the Math and Scanner classes. I have two classes, the GuessingGame class and the GuessingGameTester class.

The programs are supposed to randomly generate a number and tell the user too low/high, getting colder/warmer, and if they won/lost. The user has five guesses to guess the number between 0 and 100. If they run out of guesses or guess the correct number, they are asked if they want to play again.


Both classes compile and the tester class returns the correct results, but it will not allow the user to guess again. Instead, it asks the user if they want to play again after one guess. If the user chooses to play again, no matter what number is the user is told that they won.

Can someone please take a look at my code and point me in the right direction? Thanks!



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Welcome to the Ranch

How far have you got with that game? Can it be played at all? I can see all sorts of things. I looked at the imports (you don’t need to import java.lang.Anything, and you never use Double) and started worrying.
You have got 5 fields which you are setting up very strangely in your constructor. You ask for max as a parameter and then set it to 5, regardless of what value is passed, You are setting four other fields to their default values, rather than giving them values in the constructor.
I think you ought to initialise all the fields in the constructor; if they haven’t got default values (eg max = 5), you can pass those values as parameters.

I think you need work out what that for loop is doing. I think you should write for loops in this sort of format whenever possible for (int i = 0; i < j; i++) ... In that case, the j would appear to be max, so you can get rid of the 5 and write max.
Now, back to that loop. How many guesses does it take, and where does it take a new guess? How does newGuess alter as you go through that loop? Why is newGuess a field when it is passed to that method? Do you need the newGuess field at all? You are never using it.
In the isGameOver method (good name for the method), you are back to the newGuess field, which you have never used, so it doesn’t supply any useful information. I think you should set the gameOver field in the method which takes the guesses, and you can use the gameOver field ot help control the loop.

I think, I am afraid, that you will be starting from scratch in quite a lot of that class.
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
I agree to all the points of Ritchie. You have to think about re-structuring the logic.

- The reason why your tester class did not ask to continue is, say if i did not pick the guess first time, the guess returns a message say, "Too Low". the while loop in #22 checks if the gameOne.isGameOver is true. because i had the wrong guess, it isn't. so the flow doesn't continue into the loop and the code exits.

GuessingGame.Guess
- Why do you need a loop in the guess?
- dont you think having just one variable currentDiff would take the guesser closer to the number? Comparing the currentDiff with the previous differential may miss lead some time.

- To start with, I have re-structured just the tester class in a way to make it readable and modular. See the tester class below: I have moved the logic in main to a playGame method. To facilitate this you have to change the GuessingGame.guess to return a boolean instead of String. you can simply print the messages in the guess method itself. and work on the Guess logic.




The code above is not a perfect solution yet. Atleast it will get you into the second and further attempts which wasn't working for you before.
David Kanton
Greenhorn

Joined: Apr 23, 2012
Posts: 2
Thanks for the feedback! I took all of the advice. I'm still having problems with the loop in the GuessingGame class. I'm going to keep at it and see what I can come up with. If you have any other suggestions, I'm all ears. Thanks again!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Don’t just keep at it. Write down your algorithm and refine it until it is in words of one syllable. Once you get it to that stage, you should find the code very easy to write.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop Issues
 
Similar Threads
New Help on letting the user to choose to play again or not. here is what i have
help needed with my while loop
Play again function help?
GuessingGame
Need help ending number guessing game and asking if user wants to play again