Guess The Number is a simple number-guessing game developed in Java. The goal of the game is for the player to guess a randomly generated number between 1 and 100 in the fewest attempts possible. The game provides feedback after each guess, indicating whether the player's guess is too high, too low, or correct.
- Random Number Generation: The game generates a random number between 1 and 100 for the player to guess.
- User Input: Players can input guesses and receive feedback on whether the guess is too high or too low.
- Attempt Counter: The game tracks the number of attempts the player makes before guessing correctly.
- Game Feedback: After each guess, the player is informed whether their guess is too high, too low, or correct.
- Programming Language: Java
- Random Number Generation:
Random
class in Java - User Input:
Scanner
class for reading player input
- Java 8 or later installed on your system.
-
Clone the Repository:
git clone https://github.com/DeepakPanneerselvam1455/Guess-The-Number.git
-
Install Java:
- Download and install Java JDK if it's not already installed.
-
Run the Application:
- Open the project folder in your IDE or run the game directly from the terminal.
- To run the game via terminal:
javac GuessTheNumber.java java GuessTheNumber
- The game will generate a random number between 1 and 100.
- The player will be prompted to guess the number by entering a value between 1 and 100.
- The game will provide feedback on whether the guess is too high, too low, or correct.
- The game continues until the player guesses the correct number.
- Once the correct number is guessed, the game will display the number of attempts taken to find the correct answer.
Welcome to 'Guess the Number'!
I've selected a number between 1 and 100.
Try to guess it!
Enter your guess: 50
Too low! Try again.
Enter your guess: 75
Too high! Try again.
Enter your guess: 63
Congratulations! You guessed the correct number 63 in 3 attempts.
Thank you for playing 'Guess the Number'!
- GuessTheNumber.java: The main file that contains the logic for generating a random number, receiving user input, and providing feedback on the guess.
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
// Generate a random number between 1 and 100
int targetNumber = random.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
System.out.println("Welcome to 'Guess the Number'!");
System.out.println("I've selected a number between 1 and 100.");
System.out.println("Try to guess it!");
// Loop until the user guesses the correct number
while (guess != targetNumber) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < targetNumber) {
System.out.println("Too low! Try again.");
} else if (guess > targetNumber) {
System.out.println("Too high! Try again.");
} else {
System.out.println("Congratulations! You guessed the correct number " + targetNumber + " in " + attempts + " attempts.");
}
}
scanner.close();
System.out.println("Thank you for playing 'Guess the Number'!");
}
}
- Author: Deepak P
This project is licensed under the MIT License - see the LICENSE file for details.
For any questions or contributions, feel free to open an issue or contact the project maintainer.
- Contact: Deepak P