import java.util.Random; import java.util.Scanner; public class NumberGuessingGame { public static void main(String[] args) { // Create an instance of Random to generate a random number Random random = new Random(); int randomNumber = random.nextInt(100) + 1; // Random number between 1 and 100 int guess = 0; // Initialize guess variable int attempts = 0; // To track the number of attempts // Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the Number Guessing Game!"); System.out.println("I've picked a number between 1 and 100. Can you guess it?"); // Game loop: continues until the player guesses the correct number while (guess != randomNumber) { System.out.print("Enter your guess: "); guess = scanner.nextInt(); // Get user's guess attempts++; // Increment the attempt count if (guess < randomNumber) { System.out.println("Too low! Try again."); } else if (guess > randomNumber) { System.out.println("Too high! Try again."); } else { System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts."); } } // Close the scanner scanner.close(); } }