我是Java新手并且正在完成该术语的第一次分配。我只需运行其中3个数学技巧游戏,其中用户想到一个数字并对其进行一些计算,然后程序猜测原始数字。我被困了,因为我希望程序询问用户他是否想要在完成它时再玩一次,但不幸的是,无论我尝试什么,都会产生另一个问题。有3"难度"水平。但只要我能让一个人工作没有任何错误,就足以创造另外两个数学问题。
当我尝试实现playAgain()方法和while循环时,我的问题出现了。不幸的是,现在当用户按下取消退出游戏时,它再次询问"你想再玩一次吗?#34;你需要按NO才能实际退出。
这似乎是一个明显的答案,但我被困住了,并希望得到一些反馈我错误的地方以及如何找到解决方案。
我已经包含了我目前的代码:
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;
}
}
}
谢谢你的帮助,
答案 0 :(得分:1)
在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
}