Help needed with constructing a while loop
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.
so far this is what I have:
Code:
import java.util.*;
public class SimpleGame {
public static void main(String args[])
{
int numGuess;
int userGuess;
// 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();
while (numGuess == 1)
{
if (userGuess == 63)
System.out.print("Congratulations, you guessed the number right!");
else if (userGuess >= 63)
System.out.println("Sorry, your guess is too high");
else if (userGuess <= 63)
System.out.println("Sorry, your guess is too low");
System.out.println("The number was 63");
break;
}
while (numGuess == 2)
{
if (userGuess == 36)
System.out.print("Congratulations, you guessed the number right!");
else if (userGuess >= 36)
System.out.println("Sorry, your guess is too high");
else if (userGuess <= 36)
System.out.println("Sorry, your guess is too low");
System.out.println("The number was 36");
userGuess += 2;
numGuess += 2;
break;
}
Re: Help needed with constructing a while loop
Re: Help needed with constructing a while loop
I need help with getting it to prompt me 8 times.
Re: Help needed with constructing a while loop
This is a really bad example of a game.
Re: Help needed with constructing a while loop
Quote:
Originally Posted by
rokit boy
This is a really bad example of a game.
Not helpful.
Re: Help needed with constructing a while loop
here is the thing, when i run this, its prompts me once so i am a little confused as to how i may get it to prompt me, 2 0r 3 0r 4...etc times in a row so that is my issue, if anyone could assist me, it would be much appreciated.
thanks