Help needed with constructing a while loop please!
My task is to: 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.
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;
}
So when i run this, it prompts me once yeah but i am a bit confused as to how i may get it to prompt me 2 or 3 or 4...etc times in a row, so if anyone could assist me with this, that would be very helpful.
thanks
Re: Help needed with constructing a while loop please!
A way to accomplish that is with a do...while loop. You can first ask the user how many guesses he want and then execute the do block as many times as the guesses.
Re: Help needed with constructing a while loop please!
thank you, i have done that
Code :
public class SimpleGame {
public static void main(String args[])
{
int numGuess;
int userGuess = 0;
Scanner kbd = new Scanner(System.in);
do
{
//Prompt User for Number of guesses as well as What guess the user intends to input
System.out.print("How many guesses do you want?");
numGuess = kbd.nextInt();
System.out.println("What is your guess?");
if (numGuess == 1)
{
userGuess = kbd.nextInt();
}
if (numGuess == 2)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess == 3)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess == 4)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess ==5)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess ==6)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess ==7)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
if (numGuess ==8)
{
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
userGuess = kbd.nextInt();
}
}
while (numGuess == userGuess);
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");
}
}
now, i need to find a way to implement a infinite amount of guesses, if you could assist me with this, that would be great.
thanks
Re: Help needed with constructing a while loop please!
andreas90
I have tried to simplify the code as my previous code was a bit much
this is how it looks:
Code :
import java.util.*;
public class SimpleGme2 {
public static void main(String args[])
{
int counter = 0;
int numGuess;
int userGuess = 0;
Scanner kbd = new Scanner(System.in);
Random n = new Random ();
int number = 0;
//Prompt User for Number of guesses as well as What guess the user intends to input
System.out.print("How many guesses do you want?");
while (counter < 10)
{
number = n.nextInt();
numGuess = kbd.nextInt();
if (number == 63 )
System.out.print("Congratulations, you guessed the number right!");
break;
}
if (number >= 63)
System.out.println("Sorry, your guess is too high");
else if (number <= 63)
System.out.println("Sorry, your guess is too low");
}
}
thanks
Re: Help needed with constructing a while loop please!
Okay there is a problem with your pseudo-code. This is what your program is doing:
Code :
Do infinite number of times (note that the counter is never incremented so it will never be greater than 10)
Generate a random number (note that this is never used)
Ask the user for a number
If number == 63 print "Congratulations"
If number < 63 print "Higher"
If number > 63 print "Lower"
End do
Here is the proper pseudo-code for a higher/lower game
Code :
Do
Ask the user for the maximumNumberOfGuesses
Generate a random number (number)
Set guessCount == 0
Do
Ask the user for an input (userInput)
Increment the guess count (count)
If count > maximumNumberOfGuesses
print "You have run out of guesses. You Loose."
print "The correct answer was: " + number
exit Do
If number < userInput print "Higher"
If number > userInput print "Lower"
If number == userInput
print "Congratulations. You Win."
Ask if the user wants to play again
if true
go to start of the program
else
exit program
End do
End do
I cannot just give you the code for this as it would rob you of the learning experience. In fact providing you with the pseudo-code like this is probably the same thing because this is an exercise in structuring a program.
Do me a favor and take a look at how I analyzed the question and wrote it out as a step of instructions that a human could follow step by step. This is called pseudo-code and it is essential for structuring your programs correctly. Your code is pretty good for a new programmer but I wanted to help you out here because the code reminded me of my own when I first started; syntactically correct but schematically wrong. So PLEASE do not just look at this pseudo-code and start writing code. Go over the question again and write your own pseudo-code. Compare it to mine if you need. Then start writing code. My golden rule is; every 10 minutes of planning will save you 1hr of programming.
Re: Help needed with constructing a while loop please!
Christopher Lowe, thank you for the help, i will put your advise to good use.
Thanks once again.