• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hi-Lo guessing game  RSS feed

 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone I have been stuck on this for quite sometime. This is my first time ever taking or doing any sort of programming so any help is appreciated. My problem is the second do while I cannot get the game to be able to "play again" if the user wants to do that. I have tried different ways but I just cant seem to figure it out. Thank you for the help!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (← click that link) when posting code.


The semicolon at the end of the first line creates an infinite loop. Remove the semicolon.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard my previous comment. This is what happens when you don't format your code properly.  You also have a code block that follows the inner do-while that makes it look like the semicolor after the while is creating an infinite loop

Because of the way you formatted the code, the brace on line 9 looks like it belongs to the while on line 8.  In reality, the while completes the inner do-while loop that starts on line 3.  The opening brace on line 9 actually marks the start of a free-floating code block, which is syntactically legal but unnecessary and misleading.
 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when i remove the semicolon my whole program wont run without it. Sorry about not using CodeTags new to this website as well.

C:\Users\Home\Documents\HiLo.java:53: error: ';' expected
while (guess != SENTINEL)
                         ^
1 error

Tool completed with exit code 1


 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So should I only use one do while and have the sentinel as another if? Would that make the code more readable and correct?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of do-while is not idiomatic.  A do-while loop is used when you want to execute the body of the loop at least once.  The idiom for do-while is as follows:

If the loop condition involves user input and you want the loop to end if a sentinel value is entered, then you should be asking the user to enter a value right at the end of the loop, like so:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your inner loop will only terminate if the player enters the sentinel value as his/her guess. It won't exit when they have guessed correctly.  So, you need to have two parts to the inner while condition.

Also, printing out a message "You gave up!" doesn't make sense if the player actually guesses correctly, does it? That's what your program will do now, regardless of whether they entered the sentinel value or if they win.  So you need to make the "You gave up!" message display only when the player exits from the inner do-while loop without guessing the secret number correctly.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is actually better formatted than most beginner code we usually see posted around here.  The only thing you need to do is pull the while on line 47 close to the closing brace on line 45 because that's the brace that it belongs with, not the brace on line 48.  That block of code (lines 48-50) should be paired with an if-statement, as I mentioned in the previous reply.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:
If the loop condition involves user input and you want the loop to end if a sentinel value is entered, then you should be asking the user to enter a value right at the end of the loop, like so:


I'm afraid the above does not quite fit your scenario and I may have confused you with that example so let me walk it back.  Your inner do-while loop is like this:

The body of the loop is actually fine. The incomplete while condition is why your loop doesn't terminate properly. As I already explained before, you need another term in the condition to check if the player actually guessed correctly.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you already learned about putting code in other methods besides main()?  Because having all that code in main() is a bad practice and is not how good Java programs are written.  A better way to write that program is to break it down into smaller chunks and just have some high level code in your main() method. Something like this:

Your main method kind of gives you a high-level description of what your program will do, while all the private methods that are called from main() do the detailed work for each task that they represent.
 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried to do

and that did not work. I get what you are saying and thank you for the confidence boost about most beginning programmer. Ill try and play around for a bit more and see if I can figure it out. Thank you for all the time and the help!
 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are just learning about using other methods besides main and it was a really small program that was extremely simple even for a beginner. So I am still a little nervous about doing it that way.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a feeling that you are experiencing a bug very confusing part of the Scanner class.  Scanner#nextInt() reads and returns an integer, but leaves the newline in the buffer.  A subsequent nextInt() will ignore this extra newline, but Scanner#nextLine() will not.  It will see the newline in the buffer, "consume" (read past) it, and return an empty string.  So this code:

...will work but this code:

...will not, because someText will always be empty.  Instead, try this:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rhyeca Riley wrote:So I tried to do

and that did not work.


Well, of course it won't work. What that code is saying, in plain English, is this:  do the loop again as long as (i.e., while) the guess is not the SENTINEL value OR the guess is the answer.

So, if the player guesses the answer correctly ... what are you telling the program to do, exit the loop or do the loop again?
 
Rhyeca Riley
Greenhorn
Posts: 25
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu Lacar and Knute Snortum for all the help. My program is running now like it should thanks to you two for pointing me in the right direction. I hope some day to be helping someone with their programs like you two did for me. Much appreciated!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!