繁体   English   中英

重复方法

[英]Repeating Methods

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

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

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

但更重要的是,此功能完全可以满足您的描述。 它提出一个问题,得到答复,然后在每次调用时打印“ LEVEL ONE COMPLETE”,因为这就是您编写的方式。 另外,变量的amount是函数的局部变量,因此每次调用函数时,变量都会更新,然后在函数返回时值超出范围,从而丢弃您刚刚计算出的值。

“ amount”变量和“ LEVEL COMPLETE”消息都需要移出该函数。 您如何选择它是一个选择问题。 你可能想使变量amount静态以及(取决于你不向我们展示的代码的其余部分)。

findAdd删除最后两行

 JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
 JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);

并在调用findAdd的地方调用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);

您的金额需要写在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM