1

我的程序还没有完成,但我需要帮助来找到一种方法来运行我的方法 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;
   }
4

3 回答 3

1

首先,这会调用函数 7 次,而不是 6 次:

for (int loop = 0; loop <= 6; loop++) { findAdd() }

但更重要的是,此功能完全按照您的描述进行。它提出一个问题,得到一个答复,然后在每次调用它时打印“LEVEL ONE COMPLETE”,因为这就是你编写它的方式。此外,该变量amount是函数的本地变量,因此每次调用该函数时,它都会更新,然后当函数返回时该值超出范围,丢弃您刚刚计算的内容。

“amount”变量和“LEVEL COMPLETE”消息都需要移到函数之外。你如何做到这一点是一个选择问题。您可能还希望将变量amount设为静态(取决于您未向我们展示的其余代码)。

于 2013-06-15T18:44:48.473 回答
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);
于 2013-06-15T18:41:44.737 回答
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);
于 2013-06-15T18:44:52.190 回答