我的程序还没有完成,但我需要帮助来找到一种方法来运行我的方法 6 次。这是一个数学练习游戏,该方法进行问题的输出和答案的计算。理想情况下,该方法运行 6 次(总共 6 个数学问题),然后输出“LEVEL ONE COMPLETE”语句。但是每当我运行它时,它会在每个问题的末尾输出“LEVEL ONE COMPLETE”。此外,每次用户正确回答问题时,金额 ( int amount = 0;
) 不会增加 ( )。amount+=150;
我是初学者,因此将不胜感激!
还有一件额外的事情..如果我想在用户得到 3 个错误答案的情况下结束游戏,我应该如何将它包含到我的代码中?
谢谢!
这是我在 main 方法中调用该方法的地方.. 运行它 6 次:
for (int loop = 0; loop <= 6; loop++) { findAdd() }
这是我正在调用的方法(包含数学问题):
public static int findAdd ()
{
Object[] optionsA = {"Yes Please", "Nope! I'm good!"};
int wrong = 0;
int amount = 0;
int increment = 150;
int questionnum = 0;
questionnum ++;
int numOne = (int)(Math.random () * 30);
int numTwo = (int)(Math.random () * 30);
int answer = numOne + numTwo;
String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
int useranswer = Integer.parseInt(useranswerA);
if (useranswer != answer)
{
wrong ++;
JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrong, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (y == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (y == JOptionPane.NO_OPTION) {}
}
else if (useranswer == answer)
{
amount+=150;
JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
if (y == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (y == JOptionPane.NO_OPTION) {}
}
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
return useranswer;
}