How To Make a Number Guessing Game
As a beginner it's always very common that one wants to make some small projects, A number guessing game is one of the most popular among them.
This article shall give you an idea about how to do that.
Import this line for taking user's input.
And in the main function we declare an instance of the Scanner object for taking the user's input. And declare a variable that will hold the secret number.
int secretNumber = 3;
We can add a little bit of describtion for the user.
System.out.println("You have only 3 tries to get the correct number");
System.out.println("The secret number is between 1 and 9, Good luck!");
declare a boolean variable that will help us identify if the user guesses the correct number. and another for holding the user's guess.
int count = 0, userGuess;
do {
userGuess = inputs.nextInt();
if (userGuess == secretNumber) {
isCorrect = true;
break;
}
count++;
} while(count < 3);
The above do-while loop will only break if the user has guesses the right number or the guess limit is reached. If the user has guessed the right number we tell him he win otherwise we tell the user that he has failed and then print out the secret number.
System.out.println("You win!");
else
System.out.printf("Game Over\nSecret number is: %d\n", secretNumber);
Full source code
user's input int class*/
class GuessGame {
public static void main(String args[]) {
Scanner inputs = new Scanner(System.in);
int secretNumber = 3; //The Secret number
System.out.println("<<< Number Guessing Game >>>");
System.out.println("You have only 3 tries to get the correct number");
System.out.println("Hint: the secret number is between 1 and 9, Good luck!");
boolean isCorrect = false;
int count = 0, userGuess;
do {
userGuess = inputs.nextInt();
if (userGuess == secretNumber) {
isCorrect = true;
break;
}
count++;
} while(count < 3);
if (isCorrect)
System.out.println("You win!");
else
System.out.printf("Game Over\nSecret number is: %d\n", secretNumber);
}
}
However the above source code is not that efficient, first the user may not like the game and everytime you'll have to edit the
source code to change the secret number. To overcome these limitations we can introduce a random number generator that will generate
the secret number in a range for us, so even you can't know the secret number.
Let's edit our source code to make the program more efficient!.
Guess Game Using a Random Number Generator
import java.util.Random; //For generating a random number
class GuessGame {
public static void main(String args[]) {
Scanner inputs = new Scanner(System.in);
Random random = new Random();
//generate a random between 0 and 9 number as the secret number
int secretNumber = 1 + random.nextInt(9);
System.out.println("<<< Number Guessing Game >>>");
System.out.println("if int You have only 3 tries to get the correct number");
System.out.println("Hint: the secret number is between 1 and 9, Good luck!");
boolean isCorrect = false;
int count = 0, userGuess;
do {
userGuess = inputs.nextInt();
if (userGuess == secretNumber) {
isCorrect = true;
break;
}
count++;
} while(count < 3);
if (isCorrect)
System.out.println("You win!");
else
System.out.printf("Game Over\nSecret number is: %d\n", secretNumber);
}
}