Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Game Java

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

Game Java

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
You are on page 1/ 1

import java.util.

Random;
import java.util.Scanner;

public class NumberGuessingGame {

public static void main(String[] args) {


Random random = new Random();
Scanner scanner = new Scanner(System.in);

int secretNumber = random.nextInt(100) + 1; // Generates a random number


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

System.out.println("Welcome to the Number Guessing Game!");


System.out.println("I'm thinking of a number between 1 and 100.");

do {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;

if (guess < secretNumber) {


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

scanner.close(); // Close the scanner to prevent resource leaks


}
}

You might also like