FEATURES :
- Random Number Generation: The game generates a random number between 1 and 100 each time it starts, providing a new challenge for the player.
- User Interaction: The game takes input from the user through the console, making it interactive.
- Attempt Counting: The game keeps track of the number of attempts made by the user, adding a score-like feature to the game.
- Simple User Interface: The game runs in the console and interacts with the user through simple text prompts, making it easy to understand and play.
DETAILED BREAKDOWN :
-
Imports: java.util.Scanner: Used to get input from the user. java.util.Random: Used to generate a random number for the user to guess.
-
Main Class and Method: Class: GuessingGame Main Method: This is where the execution of the program starts.
-
Random Number Generation: A Random object is created. random.nextInt(100) + 1 generates a random number between 1 and 100 (inclusive).
-
User Interaction: Scanner object scanner is used to take input from the user. The program prompts the user to guess the number.
-
Game Loop: A while loop runs until the user guesses the correct number (userGuess != numberToGuess). The user's guess is taken using scanner.nextInt(). The number of attempts is incremented with each guess.
-
The program provides feedback: "Too low. Try again." if the guess is lower than the number. "Too high. Try again." if the guess is higher than the number.
-
End of Game: The program congratulates the user when the correct number is guessed. The total number of attempts is displayed.