0% found this document useful (0 votes)
6 views1 page

Cenoura Bnana

The document is a Java program for a Number Guessing Game where the player must guess a randomly generated number within a specified range and a limited number of attempts. The game provides feedback on whether the guess is too low or too high and ends when the player guesses correctly or exhausts their attempts. The main method initializes the game with a maximum number of 100 and allows for 10 attempts.

Uploaded by

Suraba Compl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Cenoura Bnana

The document is a Java program for a Number Guessing Game where the player must guess a randomly generated number within a specified range and a limited number of attempts. The game provides feedback on whether the guess is too low or too high and ends when the player guesses correctly or exhausts their attempts. The main method initializes the game with a maximum number of 100 and allows for 10 attempts.

Uploaded by

Suraba Compl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Random;
import java.util.Scanner;

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();
}
}

You might also like