# Create a Number Guessing Game in Java

Zeeshan Afridi May 27, 2022

A guessing game is pretty simple for the user; it is just guessing the right number within the `k` number of trials to win the game. But for the developer’s end, it is not that simple.

## Create a Number Guessing Game in Java

As a developer, you need to keep in mind the constraints and algorithms of this game.

### Guessing Game Rules

You must keep the rules in mind to develop a guessing game. To win the game, the user must satisfy the defined constraints of the game.

1. The number range can be different depending on your preference; however, for this program, the range is from 1 to 100.

2. If the guessed number is bigger than the user entered number, the program will respond with the message: `Oops! Your guess was too HIGH, Try some lower numbers`.

3. If the guessed number is smaller than the user entered number, the program will respond with the message: `Opss! Your guess was too LOW, try some higher numbers`.

4. The user has won if the user’s entered number is the same as the actual guessed number. The message that will be displayed is `Perfect! You got it Right!`.

The user will be asked to play again. He may choose to terminate the game or play again.

5. If the limit for the number of trials is reached, and the user has not guessed the number, the game will finish and display the message:

``````The number was 64
Better luck next time
Want to Play again?(y/n)
``````

The user can either play again or quit the game.

### Algorithm for Creating a Guessing Game in Java

The algorithm is a stepwise solution to a problem. The algorithm of the guessing game is quite simple.

Let’s see the steps for developing a guessing game in Java.

### Use the `Random()` Method to Generate Random Numbers in Java

Generating random numbers in Java is very easy. Java has dedicated a whole library for random numbers `java.util.Random`, and you can easily import it with the `import` keyword.

The proper syntax for importing random in Java is `import java.util.Random;`.

Example Code:

``````package articlecodesinjava;
import java.util.Random;
public class GuessingGame {

public static void main(String[] args) {

int x;
Random generator = new Random();
System.out.println("5 random numbers from 0 to 10");
for(int i = 0; i < 5; i++){
x = generator.nextInt(5);
System.out.print(x+" , ");
}
}
}
``````

Output:

``````5 random numbers from 0 to 10
3 , 1 , 3 , 4 , 4 ,
``````

### Example Code for a Guessing Game in Java

``````package articlecodesinjava;
import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {

int ans, guess, guessNum = 0;
final int maxGuesses = 5;
String str, playAgain = "y";

Scanner scan = new Scanner(System.in);
Random generator = new Random();
ans = generator.nextInt(101) + 1;

while (playAgain.equals("y") || playAgain.equals("Y")) {
System.out.println("Hey Welcome to the game \nGuess a number between 1 and 100");
System.out.println("Guess a number (0 to quit):");

guess = scan.nextInt();
guessNum = 0;
while (guess != 0)
{
guessNum++;
if (guess == ans) {
System.out.println("Perfect! You got it Right!");
break;
} else if (guess < ans)
System.out.println("Opss! Your guess was too LOW, try some higher numbers.");
else if (guess > ans)
System.out.println("Opps! Your guess was too HIGH, Try some lower numbers");
if (guessNum == maxGuesses) {
System.out.println("The number was " + ans +"\nBetter luck next time");
break;
}
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
}
System.out.println("Want to Play again?(y/n)");
playAgain = scan.next();
}
System.out.println("Thanks for playing!");
}
}
``````

Output:

``````Hey Welcome to the game
Guess a number between 1 and 100
Guess a number (0 to quit):
1
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
2
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
55
Opss! Your guess was too LOW, try some higher numbers.
Enter your guess (0 to quit):
76
Opps! Your guess was too HIGH, Try some lower numbers
Enter your guess (0 to quit):
78
Opps! Your guess was too HIGH, Try some lower numbers
The number was 64
Better luck next time
Want to Play again?(y/n)
``````

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.