Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high, try again. If the user’s guess is lower than the random number, the program should display Too low, try again. The program should use a loop that repeats until the user correctly guesses the random number.
Input: 15 (a random value that is not known before) Output: Guess a number between 1 and 100: 1 Too low, try again Guess a number between 1 and 100: 10 Too low, try again Guess a number between 1 and 100: 25 Too high, try again Guess a number between 1 and 100: 20 Too high, try again Guess a number between 1 and 100: 15 Yes, you guessed the number.
Example
Java
// Java Program to guess a Random Number Generation import java.util.Random; import java.util.Scanner; public class GFG { public static void main(String[] args) { // stores actual and guess number int answer, guess; // maximum value is 100 final int MAX = 100; // takes input using scanner Scanner in = new Scanner(System.in); // Random instance Random rand = new Random(); boolean correct = false; // correct answer answer = rand.nextInt(MAX) + 1; // loop until the guess is correct while (!correct) { System.out.println( "Guess a number between 1 and 100: "); // guess value guess = in.nextInt(); // if guess is greater than actual if (guess > answer) { System.out.println("Too high, try again"); } // if guess is less than actual else if (guess < answer) { System.out.println("Too low, try again"); } // guess is equal to actual value else { System.out.println( "Yes, you guessed the number."); correct = true; } } System.exit(0); } } |
Output

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Random vs Secure Random numbers in Java
- How to Create a Random Graph Using Random Edge Generation in Java?
- Guess Game using rand() and srand() in C
- PHP program to Generate the random number in the given range (min, max)
- How do I generate random integers within a specific range in Java?
- How to generate random number in given range using JavaScript?
- Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation
- Java Program to Implement Park-Miller Random Number Generation Algorithm
- Java Program to Generate Random Hexadecimal Bytes
- Java Program to Generate Random Numbers Using Multiply with Carry Method
- Java Program to Implement Naor-Reingold Pseudo Random Function
- Java.util.Random class in Java
- Java.util.Random.nextInt() in Java
- How to Generate a Random Directed Acyclic Graph for a Given Number of Edges in Java?
- Program to generate random alphabets
- Program to generate random string in PHP
- Generating random numbers in Java
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Java Math random() method with Examples
- A Java Random and StringBuffer Puzzle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

