100% found this document useful (1 vote)
245 views2 pages

Number Guessing Game: Source Code

The document describes a number guessing game project in Java. The game generates a random number between 1-100 and prompts the user to guess it. The computer tells the user if their guess is too high, too low, or correct. The game continues until the user correctly guesses the number. The project source code is also included.

Uploaded by

Daniela Camarasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
245 views2 pages

Number Guessing Game: Source Code

The document describes a number guessing game project in Java. The game generates a random number between 1-100 and prompts the user to guess it. The computer tells the user if their guess is too high, too low, or correct. The game continues until the user correctly guesses the number. The project source code is also included.

Uploaded by

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

3.

Number Guessing Game

The fun and easy project “Guess the Number” is a short Java project that allows the user to
guess the number generated by the computer & involves the following steps:
1. The system generates a random number from a given range, say 1 to 100.
2. The user is prompted to enter their given number in a displayed dialogue box.
3. The computer then tells if the entered number matches the guesses number or it is
higher/lower than the generated number.
4. The game continues under the user guessing the number.
You can also incorporate further details as:

• Limiting the number of attempts.


• Adding more rounds.
• Displaying score.
• Giving points based on the number of attempts.

Source Code
package guessinggame;
* Java game “Guess a Number” that allows user to guess a random number that has been
generated.
*/
import javax.swing.*;

public class GuessingGame {


public static void main(String[] args) {
int computerNumber = (int) (Math.random()*100 + 1);
int userAnswer = 0;
System.out.println("The correct guess would be " + computerNumber);
int count = 1;

while (userAnswer != computerNumber)


{
String response = JOptionPane.showInputDialog(null,
"Enter a guess between 1 and 100", "Guessing Game", 3);
userAnswer = Integer.parseInt(response);
JOptionPane.showMessageDialog(null, ""+ determineGuess(userAnswer,
computerNumber, count));
count++;
}
}

public static String determineGuess(int userAnswer, int computerNumber, int


count){
if (userAnswer <=0 || userAnswer >100) {
return "Your guess is invalid";
}
else if (userAnswer == computerNumber ){
return "Correct!\nTotal Guesses: " + count;
}
else if (userAnswer > computerNumber) {
return "Your guess is too high, try again.\nTry Number: " + count;
}
else if (userAnswer < computerNumber) {
return "Your guess is too low, try again.\nTry Number: " + count;
}
else {
return "Your guess is incorrect\nTry Number: " + count;
}
}
}

You might also like