wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help needed with my while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help needed with my while loop" Watch "help needed with my while loop" New topic
Author

help needed with my while loop

Marques Johnston
Greenhorn

Joined: Mar 19, 2012
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
What is your guess?
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

Joined: Oct 21, 2010
Posts: 67
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();
System.out.println("What is your guess?");
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Oct 13, 2005
Posts: 37981
    
  22
Welcome to the Ranch

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

Joined: Oct 13, 2005
Posts: 37981
    
  22
I see others have beaten me to it!
J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 67
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

Joined: Mar 19, 2012
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

Joined: Oct 21, 2010
Posts: 67






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

Joined: Oct 21, 2010
Posts: 67
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

Joined: Aug 22, 2008
Posts: 191

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.


Do not wait to strike till the iron is hot; but make it hot by striking....
dennis deems
Ranch Hand

Joined: Mar 12, 2011
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Mar 19, 2012
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help needed with my while loop
 
Similar Threads
Help with number guessing game
simple simple jave game
Trouble with Guessing Game
Need help ending number guessing game and asking if user wants to play again
Whille loop