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!!!
Joined: Oct 11, 2009
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.
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)?
Joined: Jul 23, 2009
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".
Joined: Oct 11, 2009
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.
here is the final code in case you would like to see how I made it work.
Joined: Jul 23, 2009
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.