File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Trouble with Guessing Game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with Guessing Game" Watch "Trouble with Guessing Game" New topic
Author

Trouble with Guessing Game

Shane Miller
Greenhorn

Joined: Oct 11, 2009
Posts: 17
I have to make a guessing game for my Java class and I cant get it to work right, Ive tried everything and it just wont work right. Everytime I fix one problem something else that worked before is messing up. The program has to do the following things.

Ask the user if they want to play
Assign a random number to a variable to be the basis of the guess
If the user is too low tell them and ask them to guess again
If there too high tell them and ask them to guess again
When they get it right, tell them how many guesses it took and then ask them if they want to play again, using 1 for yes and 0 for no.


I have narrowed it down to while loops, and if and else statements, however, the program runs fine the first time, but then on subsequent runs, it always does somehting wrong depending on what I change in the code. For example, sometimes it displays a statement I dont want it to, someitmes it ends no matter what is entered and sometimes the program says the second guess is right no matter what is entered.

Here is the code I have right now




As this code is right now, no matter what the user enters in regards to playing again, the program terminates. If I remove the break staement at the end, then the program asks the user if they want to play again, when the user hits 1 to play again, it uses 1 as the guess and then says they have it right and then asks if they want to play again.

Any help would be great I ve been working on this for like 4 hours now


Why use Windows when you can step through doors into a whole new world of computing without security holes and instability. Join us, and leave windows behind. Try Linux!!!
Shane Miller
Greenhorn

Joined: Oct 11, 2009
Posts: 17
Here is my new code, the only problem is that no matter what is entered when asked if they want ot play again the game terminates. I dont undertsand why. If the expression at the bottom of a do while loop is tested after each iteration than the fact that it is asking whether the guess is not equal to the target number AND the playAgain number is equal to the sentinal value it should run again.

I have no idea what else I can do to get this to work.

Any hlep would be great.


Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
The game terminates when the loop stops iterating. At the end of the loop, after the user has correctly guessed the value, what is
the condition of the two variables used in the loop continuation condition (guess and playAgain)?
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
Your loop continuation condition says "do not repeat this loop if guess does equal target or if playAgain does not equal sentinel."
If you carefully evaluate the values of the variables used in this condition you will see why the loop does not repeat.

I would recommend that you modify the code of GuessGame2 to play one guess game. You are very close to solving that. Once you
have that solved and it is working, figure out how to ask the user if they want to play again.

Once you have "play one guess game" figured out you could implement the whole game as follows:


Also the usual use of a sentinel value is as a flag to indicate that a process should STOP. You are using it to continue. It does the
same thing, but it is easier to follow when the loop continuation condition says "stop when stop flag is set", which would actually
be coded as "continue while stop flag is not set".

For example:

Shane Miller
Greenhorn

Joined: Oct 11, 2009
Posts: 17
Thaks alot for pointing that out. I got it working now. I guess it was a flaw in the logic of my program. I wasnt thinking how the != and == assignments were actually working in my program once I reallized that I had it up and running in about a minute.

Thanks again

here is the final code in case you would like to see how I made it work.

Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
Nice to see you got it working. I would still change the way you are using the sentinel. That's not usually the way a sentinel is used so it
is confusing to read. The idea of a sentinel is that it is "watching out for something", and will then signal it. In this case the thing being
watched for would be the user entering a 0, indicating that he doesn't want to play anymore. This relationship doesn't quite hold the way
you are using it. It doesn't make as much sense for a sentinel (think of a guard) watching out to make sure the user wants to play (enters
a 1). That's not as special. The user enters a 1 every time, unless he doesn't want to play (that's the one we care about). Think about a
guard at a bank. He's not necessarily going to say "Hello, thanks for not robbing us! Come back soon!" to every customer that doesn't rob
the bank. That would be a needless distraction. He would do better to concentrate on watching for the robber.

A couple other things. If the intention is for the computer to generate a number such that 1 <= x <= 100, then your code is not quite right. As
it is, it will generate a number 0 <= x <= 99. See this thread. Though the thread is specifically about ranges, and goes into ranges for doubles,
which are just a bit different, it is still appropriate because you do need numbers in a range (1 <= x <= 100).

Also one at least one of the variables is being reset redundantly. It is not hurting anything, but it does make the code confusing to read.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Shane,

Here is your code with only the condition at the bottom changed:



This is another option.
This is with reference to your 2nd post on this topic where you have used just a do while construct.


Priety.
Chris Burkovich
Greenhorn

Joined: Oct 23, 2009
Posts: 11
I am currently working on the same problem but here is my code

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with Guessing Game