Java - 停留在最后一点 - 尝试重播该程序

Java - Stuck on one final point - trying to replay the program

我是 Java 的新手,正在处理该学期的第一个作业。我只需要 运行 3 个数学技巧游戏,在这些游戏中,用户想到一个数字并对其进行一些计算,然后程序猜测原始数字。我被卡住了,因为我希望程序在完成后询问用户是否想再次玩,但不幸的是,无论我尝试什么,都会产生另一个问题。有 3 "difficulty" 个级别。但只要我能让它没有任何错误地工作,那么创建另外两个数学问题就足够容易了。

当我尝试实现 playAgain() 方法和 while 循环时,我的问题出现了。不幸的是,现在当用户按取消退出游戏时,它会再次询问 "would you like to play again" 并且您需要按否才能真正退出。

这似乎是一个显而易见的答案,但我被卡住了,希望得到一些反馈,告诉我哪里出错了,以及如何找到解决方案。

我已经包含了目前为止的代码:

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;
        }
    }
}   

感谢您的帮助,

像下面一样在 main 方法中应用 do-while 循环并尝试。

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
}