Advertisement
sriyanto

Guessing game

Feb 12th, 2024
1,035
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 1 0
  1. import java.util.InputMismatchException;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class GuessingGame {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Random random = new Random();
  9.         int numberToGuess = random.nextInt(100) + 1; // Random number between 1 and 100
  10.         int attempts = 0;
  11.         boolean hasGuessedCorrectly = false;
  12.  
  13.         System.out.println("Welcome to the Guessing Game!");
  14.         System.out.println("I've picked a number between 1 and 100. Can you guess it?");
  15.  
  16.         while (!hasGuessedCorrectly) {
  17.             System.out.print("Enter your guess: ");
  18.             try {
  19.                 int guess = scanner.nextInt();
  20.                 attempts++;
  21.  
  22.                 if (guess < 1 || guess > 100) {
  23.                     throw new IllegalArgumentException("Please enter a number between 1 and 100.");
  24.                 }
  25.  
  26.                 if (guess == numberToGuess) {
  27.                     hasGuessedCorrectly = true;
  28.                     System.out.println("Congratulations! You've guessed the number " + numberToGuess + " correctly.");
  29.                     System.out.println("It took you " + attempts + " attempts.");
  30.                 } else if (guess < numberToGuess) {
  31.                     System.out.println("Too low! Try again.");
  32.                 } else {
  33.                     System.out.println("Too high! Try again.");
  34.                 }
  35.             } catch (InputMismatchException e) {
  36.                 System.out.println("Invalid input. Please enter a valid number.");
  37.                 scanner.nextLine(); // Clear the input buffer
  38.             } catch (IllegalArgumentException e) {
  39.                 System.out.println(e.getMessage());
  40.                 scanner.nextLine(); // Clear the input buffer
  41.             }
  42.         }
  43.  
  44.         scanner.close();
  45.     }
  46. }
  47.  
Advertisement
Comments
  • Krahwazi
    105 days (edited)
    # text 1.94 KB | 0 0
    1. Some Ideas:
    2. Difficulty Levels: Add different difficulty levels that change the range of numbers to guess from or the number of attempts allowed. For example, easy mode could have a range of 1-50, medium mode could have a range of 1-100, and hard mode could have a range of 1-200.
    3.  
    4. Score System: Implement a scoring system based on the number of attempts it takes the user to guess the correct number. You could assign points based on the difficulty level chosen and display the user's score at the end of the game.
    5.  
    6. Hints: Allow the user to request hints after a certain number of unsuccessful attempts. For example, you could provide a hint about whether the correct number is even or odd, or if it is divisible by a certain number.
    7.  
    8. Leaderboard: Keep track of high scores and display a leaderboard showing the top players and their scores. This could add a competitive element to the game and encourage players to improve their guessing skills.
    9.  
    10. Graphics: Add graphical elements to the game, such as a simple interface with buttons and text fields, or colorful animations to make it more visually appealing.
    11.  
    12. Sound Effects: Incorporate sound effects for correct and incorrect guesses, as well as for other events in the game. This can enhance the overall user experience and make the game more immersive.
    13.  
    14. Multiple Players: Allow multiple players to take turns guessing the number, either by passing the device or implementing a multiplayer mode over a network.
    15.  
    16. Customization Options: Let the user customize certain aspects of the game, such as the background color, the appearance of the number input field, or the theme music.
    17.  
    18. Achievements and Rewards: Implement achievements or rewards for reaching certain milestones in the game, such as guessing the correct number within a certain number of attempts or reaching a high score.
    19.  
    20. Error Handling: Improve error handling to provide more informative error messages and guide the user in case of invalid input.
Add Comment
Please, Sign In to add comment
Advertisement