我的程序尚未完成,但我需要帮助才能找到一种方法来运行我的方法6次。这是一个数学练习游戏,该方法输出问题并计算答案。理想情况下,id方法运行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;
}
答案 0 :(得分:1)
首先,这会调用函数7次,而不是6次:
for (int loop = 0; loop <= 6; loop++) { findAdd() }
但更重要的是,这个功能正是你所描述的。它提出了一个问题,得到了一个回复,然后每次打电话都打印出“LEVEL ONE COMPLETE”,因为这就是你写它的方式。此外,变量amount
是函数的本地变量,因此每次调用函数时,它都会更新,然后当函数返回时值超出范围,抛弃您刚刚计算的内容。
“amount”变量和“LEVEL COMPLETE”消息都需要移出函数。你如何做到这一点是一个选择问题。您可能也希望变量amount
也是静态的(取决于您未向我们展示的其余代码)。
答案 1 :(得分:0)
从findAdd
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
并在您调用findAdd
六次的地方调用它们:
int amount; //amount goes out of the `findAdd()`
int useranswer;
for (int i=0; i<6; ++i) {
useranswer = findAdd();
if (useranswer == JOptionPane.YES_OPTION) { //quit?
return; //exit?
}
}
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
答案 2 :(得分:0)
您的金额需要在findAdd()
方法之外编写。
int amount = 0;
以下内容也需要移动到for循环后显示,而不是findAdd()
方法。
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
示例:
int amount = 0;
for (int i = 0; i < 6; i++)
{
findAdd();
}
JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);