Java Program to Guess a Random Number in a Range
Create a program that generates a random number. The program should ask 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 continue to prompt the user for their guess until they correctly guess the random number.
Example 1
The above program generates a random number using the nextInt() method from the java.util.Random class. The user is prompted to enter their guess, and the program provides feedback whether the guess is too high or too low. The program continues until the user guesses the correct number.
FileName: RandomNumberGuessing.java
import java.util.Random; import java.util.Scanner; public class RandomNumberGuessing { public static void main(String[] args) { int minRange = 1; // Minimum number in the range int maxRange = 100; // Maximum number in the range Random random = new Random(); // Create an instance of the Random class to generate random numbers int randomNumber = random.nextInt(maxRange - minRange + 1) + minRange; // Generate a random number within the specified range Scanner scanner = new Scanner(System.in); // Create an instance of the Scanner class to read user input int guess; // Variable to store the user's guess int attempts = 0; // Variable to keep track of the number of attempts made System.out.println("Welcome to the Random Number Guessing Game!"); System.out.println("Guess a number between " + minRange + " and " + maxRange); do { System.out.print("Enter your guess: "); guess = scanner.nextInt(); // Read the user's guess from the console attempts++; // Increment the attempts counter if (guess < randomNumber) { // Check if the guess is lower than the random number System.out.println("Too low! Try again."); } else if (guess > randomNumber) { // Check if the guess is higher than the random number System.out.println("Too high! Try again."); } else { // The guess is equal to the random number, so it is correct System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts."); } } while (guess != randomNumber); // Continue the loop until the user guesses the correct number scanner.close(); // Close the Scanner object to release system resources } }
Output
Welcome to the Random Number Guessing Game! Guess a number between 1 and 100 Enter your guess: 27 Too low! Try again. Enter your guess: 67 Too high! Try again. Enter your guess: 50 Too low! Try again. Enter your guess: 65 Too high! Try again. Enter your guess: 57 Too low! Try again. Enter your guess: 58 Congratulations! You guessed the correct number in 6 attempts.
Example 2
In this approach, we use a binary search algorithm to narrow down the range of possible numbers. By using a binary search approach, the algorithm efficiently narrows down the range of possible numbers based on the user's guesses, leading to quicker identification of the correct number with fewer attempts.
FileName: RandomNumberGuessing.java
import java.util.Random; import java.util.Scanner; public class RandomNumberGuessing { public static void main(String[] args) { int minRange = 1; // Minimum number in the range int maxRange = 100; // Maximum number in the range Random random = new Random(); int randomNumber = random.nextInt(maxRange - minRange + 1) + minRange; Scanner scanner = new Scanner(System.in); int guess; int attempts = 0; System.out.println("Welcome to the Random Number Guessing Game!"); System.out.println("Guess a number between " + minRange + " and " + maxRange); do { int mid = minRange + (maxRange - minRange) / 2; // Calculate the middle number in the current range System.out.print("Enter your guess: "); guess = scanner.nextInt(); attempts++; if (guess < mid) { System.out.println("Too low! Try again."); maxRange = mid - 1; // Adjust the maximum range based on the guess } else if (guess > mid) { System.out.println("Too high! Try again."); minRange = mid + 1; // Adjust the minimum range based on the guess } else { System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts."); } } while (guess != randomNumber); scanner.close(); } }
Output
Welcome to the Random Number Guessing Game! Guess a number between 1 and 100 Enter your guess: 66 Too high! Try again. Enter your guess: 40 Too low! Try again. Enter your guess: 45 Too low! Try again. Enter your guess: 50 Too low! Try again. Enter your guess: 55 Too high! Try again. Enter your guess: 54 Congratulations! You guessed the correct number in 6 attempts.