import java.util.
Random;
import java.util.Scanner;
9999000aaaa
public class NumberGuessGame {
private int numberToGuess;
private int maxAttempts;
private int attempts;
public NumberGuessGame(int maxNumber, int maxAttempts) {
this.numberToGuess = new Random().nextInt(maxNumber) + 1;
this.maxAttempts = maxAttempts;
this.attempts = 0;
}
public void play() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Try to guess the number between 1 and " + numberToGuess
+ " within " + maxAttempts + " attempts.");
while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("Congratulations! You guessed the number in " +
attempts + " attempts.");
return;
} else if (guess < numberToGuess) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
System.out.println("Game over! The correct number was: " + numberToGuess);
scanner.close();
}
public static void main(String[] args) {
NumberGuessGame game = new NumberGuessGame(100, 10);
game.play();
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("Hi, my name is " + name + " and I am " + age + " years
old.");
}
}
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
class Animal {
private String species;
public Animal(String species) {
this.species = species;
}
public void makeSound() {
System.out.println("The " + species + " makes a sound.");
}
}
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public void displayInfo() {
System.out.println("Title: " + title + ", Author: " + author);
}
}
class Car {
private String model;
private int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void drive() {
System.out.println("Driving a " + year + " " + model);
}
}
class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double area() {
return width * height;
}
}