Guess The Number Game in Java — 1

Minhajul Alam
3 min readDec 10, 2018

--

Game Idea: In this game, our program will generate a random number and the task for the user is to guess the number.

If the user guess correctly we will show a message “Congratulation”.

If the user guess higher number we will show a message “Guess Lower”.

If the user guess lower number we will show a message “Guess Higher”.

The game will continue until user guess the correct number.

For example, let’s assume that our program randomly select number 3. Now user first input is 1, so our programs output will be “Guess Higher”, now user second input is 4, so our programs output will be “Guess Lower”. Now if the user input is 3 then our programs output will be “Congratulation” after that our program will be stop. Let’s Build the Game…

Build Process

  1. First of all we will generate a random number from 1 to 10.

2. Then we will build a Scanner.

3. Then we will create a while loop and we will take input from user and check those conditions to show messages to the user. That’s it.

What you should know

Variable, if else, loop, break statement, How to take input from user

If you don’t know how to Generate Random Numbers in java please read this 2 minute article.

Implementation

1. First of all we will generate a random number from 1 to 10.

import java.util.Random;

public class GuessTheNumber {
public static void main(String args[]){

//creating instance of Random() class
Random rand = new Random();

//creating a int type variable
int randomInteger;

//storing random number in randomInteger variable
randomInteger = rand.nextInt(10)+1;

}
}

2. Now we will build a scanner to take input from user.

Scanner scanner = new Scanner(System.in);

Now we have to import java.util.Scanner; in the top of our program.

3. We will create a while loop and we will take input from user and check those conditions to show messages to the user.

while(true){


System.out.print("User Input:");
//storing user input in userInput variable
int userInput = scanner.nextInt();

//checking conditions and showing messages
if(userInput==randomInteger){
System.out.println("Congratulation");
break;
}else if(userInput>randomInteger){
System.out.println("Guess Lower");
}else{
System.out.println("Guess Higher");
}
}

We set condition true in while loop because we want to stop this loop when user input same as randomInteger number. We use break statement in the if block to jump from this while loop.

Full code:

import java.util.Random;
import java.util.Scanner;

public class GuessTheNumber {
public static void main(String args[]){

//creating instance of Random() class
Random rand = new Random();

//creating a int type variable
int randomInteger;

//storing random number in randomInteger variable
randomInteger = rand.nextInt(10)+1;

Scanner scanner = new Scanner(System.in);

while(true){


System.out.print("User Input:");
//storing user input in userInput variable
int userInput = scanner.nextInt();

//checking conditions and showing messages
if(userInput==randomInteger){
System.out.println("Congratulation");
break;
}else if(userInput>randomInteger){
System.out.println("Guess Lower");
}else{
System.out.println("Guess Higher");
}
}
}
}

Output:

User Input:1
Guess Higher
User Input:6
Guess Lower
User Input:5
Guess Lower
User Input:4
Guess Lower
User Input:3
Guess Lower
User Input:2
Congratulation

Now in Guess The Number in Java — 2 we will add more features in our program.

--

--