Develop a guessing game in Java, where a user has K attempts to guess a randomly generated number. Generates a random number say between 1 and 50 and allows the user to guess the number within the generated number of attempts.
It provides feedback based on whether the guessed number is higher or lower than the actual number and ends with a message if the user exhausts all the trials without guessing the correct number. The game follows these guidelines:
- If the guessed number is larger than the actual number, the program will notify the user that the guessed number exceeds the actual number.
- If the guessed number is smaller than the actual number, the program will inform the user that the guessed number is below the actual number.
- If the guessed number matches the actual number or if the user exhausts the K attempts, the program will conclude with an appropriate message.
How to implement guessing game in java?
- Generate a Random Number: Use Java’s random number generator to produce a random number within a defined range.
- Receive User Input: Prompt the user to enter their guessed number.
- Compare the Guessed Number with the Random Number:
- If the guessed number is higher, inform the user it’s greater.
- If the guessed number is lower, inform the user it’s lesser.
- If the guessed number matches the random number, end the game with a success message.
- Keep track of the attempts made.
- Iterate through Steps 2-3 for K Trials: Allow the user a limited number of attempts (K trials) to guess the number.
- End of Trials:
- If the user exhausts all trials without guessing correctly, conclude the game by revealing the correct number.
- Offer an option to replay if desired.
- Ensure Input Validation: Validate user input to handle non-numeric inputs or guesses outside the valid range.
Program: Develop Guessing game in java
import java.util.Scanner; import java.util.Random; public class NumberGuessingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int maxRange = 50; // You can change this to set the maximum range for the random number int maxAttempts = 5; // Change the number of attempts (K trials) allowed int randomNumber = random.nextInt(maxRange) + 1; int attempts = 0; boolean hasGuessed = false; System.out.println("Welcome to the Number Guessing Game!"); System.out.println("Please select a number between 1 and " + maxRange + "."); System.out.println("You have " + maxAttempts + " trials to guess it."); while (attempts < maxAttempts && !hasGuessed) { System.out.print("Enter your guess: "); int guess = scanner.nextInt(); attempts++; if (guess == randomNumber) { hasGuessed = true; System.out.println("Congratulations! You've guessed the number in " + attempts + " attempts."); } else if (guess < randomNumber) { System.out.println("The guessed number is lower than the actual number."); } else { System.out.println("The guessed number is higher than the actual number."); } } if (!hasGuessed) { System.out.println("Sorry, you've exhausted all your trials. The number was: " + randomNumber); } scanner.close(); } }
Output: Guessing game in java
Welcome to the Number Guessing Game! Please select a number between 1 and 50. You have 5 trials to guess it. Enter your guess: 30 The guessed number is higher than the actual number. Enter your guess: 20 The guessed number is higher than the actual number. Enter your guess: 10 The guessed number is lower than the actual number. Enter your guess: 13 The guessed number is lower than the actual number. Enter your guess: 16 Congratulations! You've guessed the number in 5 attempts.