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

Java Number Guessing Game Code

The document contains a Java program for a Number Guessing Game where the player guesses a randomly generated number between 1 and 100. The program provides feedback on whether the guess is too low or too high and counts the number of attempts. The game continues until the player correctly guesses the number, after which it congratulates the player and closes the scanner.

Uploaded by

shadowsvj63
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)
7 views1 page

Java Number Guessing Game Code

The document contains a Java program for a Number Guessing Game where the player guesses a randomly generated number between 1 and 100. The program provides feedback on whether the guess is too low or too high and counts the number of attempts. The game continues until the player correctly guesses the number, after which it congratulates the player and closes the scanner.

Uploaded by

shadowsvj63
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

import [Link].

Random;
import [Link];

public class NumberGuessingGame {

public static void main(String[] args) {


Random random = new Random();
Scanner scanner = new Scanner([Link]);

int secretNumber = [Link](100) + 1; // Generates a random number


between 1 and 100
int guess;
int attempts = 0;

[Link]("Welcome to the Number Guessing Game!");


[Link]("I'm thinking of a number between 1 and 100.");

do {
[Link]("Enter your guess: ");
guess = [Link]();
attempts++;

if (guess < secretNumber) {


[Link]("Too low!");
} else if (guess > secretNumber) {
[Link]("Too high!");
} else {
[Link]("Congratulations! You guessed the number in " +
attempts + " attempts.");
break;
}
} while (true); // Continues until the player guesses correctly

[Link](); // Close the scanner to prevent resource leaks


}
}

You might also like