Professional Documents
Culture Documents
GUESSING
GAME
TRISHAL SAINI
219301329
NUMBER GUESSINGG GAME
OBJECTIVE –
The number guessing game is a fun little time pass game made on java the main purpose
behind making this game was to gain some intrest in java and learn along the way.
How it works-
First it generates a random number bettween 1-100 . The guesser now have to guess
a number between 1-100 the program is then made to search if the guessed number
is either higher or lower than the number the player has guessed . Once its done it is
instructed to then tell the player if the random number guessed by the player is
either correct lower or higher than the number that the system has generated. If the
guess is not correct than the player is instructed to guess again until it is correct
which was acomplished by using a while loop if it had noot been there the program
would have ended after just one guess which will be a boring game . also it it
programmed to tell the player that how many tries he ttook before he guessed the
number correctly .
______________________________CODE-
__________________________________
import java.util.Random;
import java.util.Scanner;
int tryCount = 0;
while(true) {
System.out.println("Enter your guess (1-100):");
if (playerGuess == randomNumber) {
System.out.println("Correct! You Win!");
System.out.println("It took you " + tryCount + " tries");
break;
}
else if(randomNumber > playerGuess) {
System.out.println("Nope! The number is higher. Guess again.");
}
else {
System.out.println("Nope! The number is lower. Guess again.");
}
}
scanner.close();
}
}
SNAPSHOT-