import java.util.
Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int secretNumber = random.nextInt(100) + 1; // Generates a random number
between 1 and 100
int guess;
int attempts = 0;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I'm thinking of a number between 1 and 100.");
do {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("Too low!");
} else if (guess > secretNumber) {
System.out.println("Too high!");
} else {
System.out.println("Congratulations! You guessed the number in " +
attempts + " attempts.");
break;
}
} while (true); // Continues until the player guesses correctly
scanner.close(); // Close the scanner to prevent resource leaks
}
}