Java——在最后一点上卡住了——试图重播程序



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

当我试图实现playAgain()方法和while循环时,我的问题就出现了。不幸的是,现在当用户按下"取消"退出游戏时,它会再次询问"你想再次玩吗",而你需要按下"否"才能真正退出。

这似乎是一个显而易见的答案,但我陷入了困境,希望能得到一些反馈,告诉我哪里出了问题,以及该怎么做才能找到解决方案。

到目前为止,我已经包含了以下代码:

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
}

相关内容

最新更新