I am new to Java and working on the first assignment of the term. I simply have to run 3 of those math trick games where the user thinks of a number and does some calculations to it and then the program guesses the original number. I am stuck because I want the program to ask the user if he wants to play again when he is done going through it, but unfortunately whatever I try just creates another problem. There are 3 "difficulty" levels. but as long as i can get the one working without any errors than it is easy enough to create two other math questions.
My problem arises when i tried to implement the playAgain() method and a while loop. Unfortunately, now when the user presses cancel to quit the game it asks again "would you like to play again" and you need to press NO for it to actually quit.
It seems like such an obvious answer to this, but i am stuck and was hoping to get some feed back on where I am going wrong and what to do to find a solution.
I have included the code I have so far below:
import javax.swing.JOptionPane;
public class AssignmentBeta
{
public static void main(String [] args)
{
int answer = JOptionPane.showConfirmDialog
(null, "Would you Like to Play a Math
Guessing Game?", "Play a Game!",
JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION)
{
String gamelevel = (String) JOptionPane.showInputDialog
(null,"please choose a difficulty level",
"Choosing The Level",
JOptionPane.QUESTION_MESSAGE, null ,
new Object[] {"Amazingly Hard!",
"Boringly Mediocre", "Shockingly Easy"},
"Shockingly Easy");
if(gamelevel==null)
{
JOptionPane.showMessageDialog(null, "Goodbye"); //CANCEL
}
else
{
Rungame(gamelevel);
}
}
else
{
JOptionPane.showMessageDialog(null, "Goodbye"); //CANCEL
}
//playAgain=playAgain(); -- this is where i am implementing it
}
public static void Rungame(String gamelevel)
{
if(gamelevel.equals("Amazingly Hard!"))
{
System.out.print("Dark side of the Moon");
}
else if(gamelevel.equals("Boringly Mediocre"))
{
int age =0;
boolean retry = true;
JOptionPane.showMessageDialog(null, "I Will Guess
Your Age!");
JOptionPane.showMessageDialog(null, "Multiply the
FIRST DIGIT of your AGE by 5.");
JOptionPane.showMessageDialog(null, "Add THREE to
the number.");
JOptionPane.showMessageDialog(null, "DOUBLE UP the
number.");
JOptionPane.showMessageDialog(null, "Now ADD the
SECOND DIGIT of your age to the number.");
while(retry || !(age > 8 && age <= 105))
{
String number = "";
while(number.isEmpty())
{
number = JOptionPane.showInputDialog(
null,
"Please enter the NUMBER and I
will return your age!",
"Multiplication Testing",
JOptionPane.QUESTION_MESSAGE);
if(number == null)
{
JOptionPane.showMessageDialog(null,
"Goodbye"); //CANCEL
}
else if (number.isEmpty())
{
JOptionPane.showMessageDialog(null, "You
didn't enter a value."); //NO VALUE
}
}
age = ( Integer.parseInt(number) - 6);
if(!(age >8 && age <=105))
{
JOptionPane.showMessageDialog(null, "Don't
lie, that is all but impossible!");
}
else
{
JOptionPane.showMessageDialog(null, "You are
Only " + age + " Years Young!!" );
retry = false;
}
}
}
else
{
System.out.print("suck my kiss");
}
}
public static boolean playAgain()
{
int answer = JOptionPane.showConfirmDialog(null, "Do you want to
play again?", "Continue?",
JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.YES_OPTION)
{
return true;
}
else
{
return false;
}
}
}
Thank you kindly for your help,
apply do-while loop in main method just like below and try.
if(answer == JOptionPane.YES_OPTION)
{
do {
String gamelevel = (String) JOptionPane.showInputDialog
(null,"please choose a difficulty level",
"Choosing The Level",
JOptionPane.QUESTION_MESSAGE, null ,
new Object[] {"Amazingly Hard!",
"Boringly Mediocre", "Shockingly Easy"},
"Shockingly Easy");
if(gamelevel==null)
{
JOptionPane.showMessageDialog(null, "Goodbye"); //CANCEL
}
else {
Rungame(gamelevel);
}
} while(playAgain());
}
else
{
JOptionPane.showMessageDialog(null, "Goodbye"); //CANCEL
}