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.
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