Number Guessing Game in Java

In Java, a number guessing game is a basic game in which the computer creates a random number and the player attempts to guess it within a specific range. Here's a quick rundown of how it works:

  • The game begins with the computer creating a random number within a given range (for example, between 1 and 100).
  • The following number is guessed by the player: The participant is asked to enter their best prediction for the random number.
  • Contrast the following guesses: The computer then compares the player's guess against the number created at random.
  • Provide input: The computer delivers feedback to the player based on the comparison. It may indicate whether the guess is too high or too low, or if the guess is right.
  • Repeat or end: The player makes guesses until they predict the proper number, at which time the game terminates with a celebratory message.

Game Rules

To create a Java software that gives the user K chances to correctly predict a randomly generated number. The game's rules are listed below:

  • The software will answer by stating that the predicted number is greater than the real number if it is larger than the actual number.
  • The software will answer with the message that the predicted number is lower than the real number if it is smaller than the actual number.
  • The program will terminate with the appropriate message if the guesses are accurate or if all K trials have been performed.

Approach

  • The way is to produce a random integer in Java using the Math.random() function.
  • Now, using a loop, accept K inputs from the user and publish whether the number is lower or bigger than the real number for each input.
  • If the user accurately estimated the number within K tries, report that the user won.
  • Otherwise, publish that he was unable to predict, followed by the actual number.

Number Guessing Game Java Program

NumberGuess.java

Output:

Welcome to the Number Guessing Game!
A number has been chosen between 1 and 100.
Your task is to guess the number within 5 attempts.
Attempt 1: Enter your guess:
3
The secret number is greater than your guess.
Attempt 2: Enter your guess:
50
The secret number is less than your guess.
Attempt 3: Enter your guess:
30
The secret number is less than your guess.
Attempt 4: Enter your guess:
20
The secret number is greater than your guess.
Attempt 5: Enter your guess:
24
You have exhausted all 5 attempts.
The secret number was 29

Let's see another approach for the same.

NumberGuess.java

Output:

I'm thinking of a number between 1 and 100.
You have 5 attempts to guess the number.
Enter your guess: 
5
Your guess is too low. You have 4 attempts left.
Enter your guess: 
25
Your guess is too low. You have 3 attempts left.
Enter your guess: 
50
Your guess is too low. You have 2 attempts left.
Enter your guess: 
70
Your guess is too low. You have 1 attempts left.
Enter your guess: 
90
Your guess is too high. You have 0 attempts left.
You've run out of attempts. You lose!





Latest Courses