Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Guessing Game While Loop  RSS feed

 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me with this. I am a gumshoe in java. I can't figure out why my while loop is not working. I would like to be able to keep choosing a number for 3 tries.

 
praveen kumaar
Ranch Hand
Posts: 456
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Donaldson!
Welcome to CodeRanch!
the problem is at line 17 try to figure it out by yourself.

Hint:check the variable numberOftries in the condition of while...

Kind regards,
Praveen.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will changing it to this below work?



}
 
praveen kumaar
Ranch Hand
Posts: 456
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And 1 more thing the variable randomNum will always be 0 as you use the bound=1 which is declared exclusive in API(of Random).so you should correct it.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so here is the new program:
I would want the user to be able to try to choose a number 3 times. The program quits after 1 try. The last few lines is the output of the program. I have no idea why it stops after one try.



Hello Welcome to the Guessing game!
Please Choose a number 0 - 10.
2
Try Again
 
praveen kumaar
Ranch Hand
Posts: 456
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Donaldson wrote:Will changing it to this below work?
yeah it will work but it's not a good soln.you should better use "<=" instead of "==" operator because using the variable numberOftries in a while condition means you need to limit the user with the attempt(accordingly as i think).
think about it!

kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 456
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Donaldson wrote:I would want the user to be able to try to choose a number 3 times. The program quits after 1 try. The last few lines is the output of the program. I have no idea why it stops after one try.
that's because numberOftries==3 will be true only for 1st iteration afterwards the numberOftries will be incremented by 1 which makes it 4 and the expression numberOftries==3 will become false and thus the loop will break.even though if you will make it numberOftries<=3,it will again make the loop to break after 1st iteration as due to the increment the expression becomes 4<=3(for 2nd iteration) which is again false.try to solve it by yourself.please show us when you are done.

Hint: Do some correction for asssignment of numberOftries and also with the conditional expression of numberOftries. 
Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 456
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Their is another problem with playeroneguess and randomNum.suppose the user guess the correct number in the first attempt then the expression inside the while loop-PlayerOneguess != randomNum will be false which makes it to not start the loop.
2nd thing is suppose the user make a wrong guess then you are required to ask him again but your while loop is comparing the same old guess and thus the user can never succeed.
so do the corrections by analysing the part i have mentioned.

Kind regards,
Praveen.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I am completely lost now. I having been staring at this program for about 15 minutes and cannot figure what is wrong with it.




Here is the output now:

Hello Welcome to the Guessing game!
Please Choose a number 0 - 10.
6
Try Again
Try Again
Try Again

 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not getting user input inside the while loop.
 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost got the code tags correct, but you need to surround the code with the code tags.  See UseCodeTags (that's a link).
 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Playerguess" and PlayerOneguess" are not good variable names.  First, a variable name by convention start with a lowercase letter.  Second, the names don't describe what the variable are, in fact, "Playerguess" is very misleading.  Try something like:
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I receive input from the user within a while loop?
Can i just copy and paste or do I need to declare a new variable.

 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about line 21 for a second.  Do you want a new variable in the while loop or do you want to reuse the same variable?
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want a new variable for each guess the user enters.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code for the program. I am unsure how to get a new user input within a while loop.


 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the new Scanner on line 21, just use the one you already have from line 14.

Variable names should start with lowercase letters, so PlayerGuess and PlayerGuess1 should start with a lowercase letter p. Also, you don't need a second variable for player guess. Just use the one you declare on line 15.

You don't need that if statement on line 23-25 either.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the following code below gives me an error for duplicate variable name. playerGuess is giving the error for duplicate variable.

 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you shouldn't redeclare the type on line 21. You've already declared that variable as an int earlier on line 15, you shouldn't declare it again on line 21. Just use the variable without declaring it as an int again.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can rearrange your code in main() into more logical groupings:

Note which statements I have group together and separated with a blank line.

Also note the change I made to the while statement on line 17. This is idiomatic of Java programs that use 0-based counts/indices. It makes more sense to read the code written this way since your logic is to limit to 3 tries, not 2.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I added a if statement to prompt a message to the command line for the correct number. I am sure there is a better way to doing this. But if it complies I am happy.

 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put the if statement outside of you're while statement
 
Knute Snortum
Sheriff
Posts: 3842
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You can put the if statement outside of you're while statement

It is, the format is just bad.
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does using the format option in Eclipse do a good formatting job. That is what I have been using to format the code.


 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the ending code. I need to learn more about OO but I guess that comes in time.

Thanks everyone for all your help!!

 
Carey Brown
Bartender
Posts: 2710
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will return an int in the range of 0 - 9 (inclusive) which does not include '10' like your prompt suggests.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Donaldson wrote:I need to learn more about OO but I guess that comes in time.

And there's no better time than now. I think you should start with objects early. If you keep writing programs in a procedural style, which is what you're doing now, it will only make the shift to objects more difficult.

See my comments in this thread: https://coderanch.com/forums/posts/preList/674141/3156502#3156502
 
M Donaldson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu, I will start reading up on OO programming.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!