The following documentation gives instruction on how to get started and play the number guessing game.
This program works for both IOS and Windows OS.
This game will run in Eclipse. Prior to downloading eclipse you will want to check if you have Java JDK installed The easiest way to check if you have Java JDK installed is
- Opening Command Prompt
- Typing in “java -version” into your terminal and hitting enter
- This will tell you the version of java you have installed, if you do not have Java JDK installed then it will not show what version you have.
![](https://cdn.discordapp.com/attachments/833448220620488795/910998507958177792/command.png) - If you need to download [Java JDK](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) then click on the link and follow the instructions from there. ##### Once you have [Java JDK](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) Installed 1. Begin by downloading [Eclipse](https://www.eclipse.org/downloads/packages/release/kepler/sr1/eclipse-ide-java-developers) ![](https://cdn.discordapp.com/attachments/833448220620488795/911003955184693308/Copy.png)
Open the .exe file located in the .zip folder
Now you have successfully downloaded and opened eclipse
The website that the source code is obtained from did not provide a .zip file for the game. So you will need to
When prompted to create “module-info.java” file select “Don’t Create”.
Next you will double click the file “numberGuessingGame”, right click on the “src” file and select “new” then “class”.
package numberGuessingGame;
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;
}
}
}
import javax.swing.*;
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++;
}
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;
}
Yes! You will need to have a compiler that can run java such as Eclipse.
You can find the original source code from the page >hackr.io.
No, you should be able to follow this documentation and play this game without any background in java.
If you run into problems you can always email me at: robertscc1@appstate.edu
If you would like to contribute to the project, then contact me at robertscc1@appstate.edu.
This project is distributed by an MIT license.