# help needed with my while loop

Marques Johnston
Greenhorn
Posts: 4
good day all, i have been having problems trying to construct a while loop, it is my first time and i am clueless as to how to get it to work.
Here are my instructions:
You are to write a complete Java program that plays a simple game. The game simply chooses a
random number between 1 and 100, and then has the user make several attempts at guessing the
number. Here’s the outline of what your program should do:
1. Prompt the user for and read in the number of guesses the user wants at the random number.
2. Generate (but not immediately display) the random number between 1 and 100.
3. As long as the user has not yet guessed the random number and the number of guesses
allowed has not been exceded:
• Prompt the user to enter their next guess at the generated random number.
• If the guess is smaller than the actual random number, print out a message indicating
that the user’s guess is too small.
• If the guess is larger than the actual random number, print out a message indicating
that the user’s guess is too large.
4. If the user succeeded in guessing the random number, then print out a congratulatory
message and tell them how many guesses they took.
5. If the user did not succeed in guessing the random number, the number should be displayed.

This is my code:

When i run this, lets say for 5 guesses, it does this:
How many guesses do you want?5
56
How many guesses do you want?

when it is actually supposed to do this:

How many guesses do you want? 5
Guess #1: What is your guess? 50
Sorry - your guess is too low.
Guess #2: What is your guess? 75
Sorry - your guess is too high.
Guess #3: What is your guess? 63
Congratulations - you guessed the number!!
It took you 3 guess.

so if anyone could assist me as soon as possible with this, that would be very helpful.

Thank you

J Miller
Ranch Hand
Posts: 80
without actually running it, i tlooks like your problem is that the prompt is inside the loop. You should move this:

//Prompt User for Number of guesses as well as What guess the user intends to input
Scanner kbd = new Scanner(System.in);
System.out.print("How many guesses do you want?");
numGuess = kbd.nextInt();
userGuess = kbd.nextInt();

to BEFORE the beginning of the while loop (before "do"). Also, the code that you want to repeat should be between "do" and "while". So it should be:

prompt user
do
{
code to repeat
} while (condition)

Matthew Brown
Bartender
Posts: 4568
9
Hi Ben. Welcome to The Ranch!

Without looking into it in too much detail, two things stand out. Firstly, were you planning to use a do/while loop or a while loop? The code looks a bit confused between the two. Here's an excerpt with some comments:

Secondly, this line:
Is an empty while loop. It will either do nothing or go into an infinite loop of doing nothing, depending on the values of numGuess and userGuess.

Campbell Ritchie
Sheriff
Posts: 50175
79
Welcome to the Ranch

You have got ; at the end of a line starting while. That is almost always a mistake.

Campbell Ritchie
Sheriff
Posts: 50175
79
I see others have beaten me to it!

J Miller
Ranch Hand
Posts: 80
This will do the job. You had it mostly right, just in the wrong places. Also, try to remember to use the brackets {} when doing an if-statement. It makes it MUCH easier to read when you start getting into more complicated code. Trust me.

Marques Johnston
Greenhorn
Posts: 4
Thank you for the welcome.

J Miller, thanks for the help, before you posted this, i reviewed my code and came up with this:

now, the else and the semi-colon after break are underlined, a bit confused as to what to do here?

J Miller
Ranch Hand
Posts: 80

Remove the semi-colon after if(true). Putting the semicolon there ends the if-statement, so Java doesn't know what to do with the else.

J Miller
Ranch Hand
Posts: 80
also, you have 2 "do" statements, and only one "while". And that "while" is nested inside an if-statement. The if-statement will also never resolve to false because it's

so the else will never get called.

Manoj Kumar Jain
Ranch Hand
Posts: 198
Hi,

First of all place the logic for if block inside the {}, as code is not doing what it is supposed to do. (Search what happens if we don't put { after if/else block)
also syntax/logic at the last in your program is not correct, I will suggest you to first make a program that make user play the game only once.
Once you are done with that now add the functionality to ask user
"Do you want to play again?"

First make your program run with minimum complexities.

dennis deems
Ranch Hand
Posts: 808
I recommend starting over from scratch. Break down your problem into small, self-contained tasks. Solve each task individually. Solve it on paper first, away from the computer. Only then begin to write Java code, and test that it works. Only after you know each element of your problem is working independently should you bring them all together into a single program. You need to get a handle on loops, so to start, I suggest writing a small piece of code that executes a simple while loop and does nothing more than that.

Matthew Brown
Bartender
Posts: 4568
9
Well, you've got a few problems there.

Firstly "End do" is not Java syntax.

Most of your other problems are down to not including the { } brackets, as J Miller suggested. That, and some extra semi-colons you've got that shouldn't be here. Here are two examples from your code:
actually means

And actually means:

You have to put { }s in to put more than one line in a block. And I'd strongly suggest using them even if you only have one line in a block, as you're less likely to make mistakes like this.

Matthew Brown
Bartender
Posts: 4568
9
And I'd definitely agree with the suggestions to leave off complicated logic until you're comfortable getting simpler cases to work.

Marques Johnston
Greenhorn
Posts: 4
Apprecaite the help everyone,
that was really helpful, hopefully as i get used to loops, i will remeber to include the brackets.

Thank you all