import java.util.Random;
import java.util.Scanner;
// GuessingGame class represents the game itself
public class GuessingGame {
private final int secretNumber;
private boolean isGameOver;
public GuessingGame() {
Random random = new Random();
secretNumber = random.nextInt(100) + 1; // Generate a random number between 1 and 100
isGameOver = false;
}
public void play() {
System.out.println("Welcome to the Guessing Game!");
while (!isGameOver) {
int guess = getGuessFromPlayer();
checkGuess(guess);
}
System.out.println("Congratulations! You guessed the number!");
}
private int getGuessFromPlayer() {
System.out.print("Enter your guess: ");
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
private void checkGuess(int guess) {
if (guess == secretNumber) {
isGameOver = true;
} else if (guess < secretNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
}
// Main class to start the game
public class Main {
public static void main(String[] args) {
GuessingGame game = new GuessingGame();
game.play();
}
}