import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int secretNumber = (int) (Math.random() * 100) + 1;
int maxAttempts = 10;
int attempts = 0;
boolean hasGuessedCorrectly = false;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Try to guess the secret number between 1 and 100.");
System.out.println("You have " + maxAttempts + " attempts.");
while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int guess = scanner.nextInt();
if (guess == secretNumber) {
System.out.println("Congratulations! You've guessed the number: " + secretNumber);
hasGuessedCorrectly = true;
break;
} else if (guess < secretNumber) {
System.out.println("Your guess is too low. You have " + (maxAttempts - attempts - 1) + " attempts left.");
} else {
System.out.println("Your guess is too high. You have " + (maxAttempts - attempts - 1) + " attempts left.");
}
attempts++;
}
if (!hasGuessedCorrectly) {
System.out.println("Game over! The secret number was: " + secretNumber);
}
scanner.close();
}
}
NumberGuessingGame.main(null);
Welcome to the Number Guessing Game!
Try to guess the secret number between 1 and 100.
You have 10 attempts.
Enter your guess: Your guess is too low. You have 9 attempts left.
Enter your guess: Your guess is too high. You have 8 attempts left.
Enter your guess: Your guess is too high. You have 7 attempts left.
Enter your guess: Your guess is too low. You have 6 attempts left.
Enter your guess: Your guess is too high. You have 5 attempts left.
Enter your guess: Your guess is too high. You have 4 attempts left.
Enter your guess: Congratulations! You've guessed the number: 29