Can anyone help me with this piece of Java. I need to create a random number game where the user has three guesses at a number between 1 and 10. When running the game, if the user has guessed incorrectly 3 times, the message saying they have lost does not appear. Also, if they guess correctly on their first guess, it still asks them to guess and then the message comes up at the end stating they have had too many guesses. Please let me know if anyone can help me out with this.
PHP Code:
import javax.swing.JOptionPane;
public class RandomNumberGameQ1{
public static void main (String args[]){
String userGuess1,userGuess2,userGuess3;
int number1,number2,number3,randomNumber;
String output="";
boolean match=false;
//Generate a random number between 1 and 10
randomNumber = (int) (Math.random() * 10) + 1;
//Prompt user for their first guess
userGuess1 = JOptionPane.showInputDialog("Please guess a number between 1 and 10");
//Convert string to integer
number1 = Integer.parseInt(userGuess1);
//Compare users first guess to random number
match = checkGuess(number1, randomNumber);
if (match == true){
output = "You Won, The random number was " + randomNumber;
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End if
else {
output = "Try Again";
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End else
if (match == false) {
//Prompt user for their second guess
userGuess2 = JOptionPane.showInputDialog("Please guess another number between 1 and 10");
//Convert string to integer
number2 = Integer.parseInt(userGuess2);
//Compare users second guess to random number
match = checkGuess(number2,randomNumber);
if (match == true){
output = "You Won, The random number was " + randomNumber;
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End if
}//End if
else{
output = "Try Again";
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End else
if (match == false) {
//Prompt user for their Final guess
userGuess3 = JOptionPane.showInputDialog("Please guess your final number between 1 and 10");
//Convert string to integer
number3 = Integer.parseInt(userGuess3);
//Compare users final guess to random number
match = checkGuess(number3,randomNumber);
if (match == true){
output = "You Won, The random number was " + randomNumber;
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End if
}//End if
else{
output = "Sorry, You have guessed incorrectly too many times. The random number was " + randomNumber;
//print message
JOptionPane.showMessageDialog(null, output, "Random number result",
JOptionPane.INFORMATION_MESSAGE);
}//End else
}//End main
//Method to check the users guess to the random number
public static boolean checkGuess(int userGuess, int randomNumber){
boolean match = false;
if (userGuess == randomNumber)
match = true;
return match;
}//End method
}
Social Networking Bookmarks