简体   繁体   English

重复方法

[英]Repeating Methods

My program isn't finished yet but I need help in finding a way to run my method 6 times. 我的程序尚未完成,但是我需要帮助找到一种方法来运行我的方法6次。 It is a math drill game where the method does the outputting of the question and calculating of the answer. 这是一个数学练习游戏,其中该方法负责输出问题并计算答案。 Ideally id like for the method run 6 times (6 math questions in total), then afterwards output the "LEVEL ONE COMPLETE" statement. 理想情况下,id为该方法运行6次(总共6个数学问题),然后输出“ LEVEL ONE COMPLETE”语句。 But whenever I run it, It outputs "LEVEL ONE COMPLETE" at the end of every question. 但是,只要我运行它,它就会在每个问题的末尾输出“ LEVEL ONE COMPLETE”。 Also, the amount of money ( int amount = 0; ) does not increment ( amount+=150; ) each time the user gets the question right. 同样,每次用户正确回答问题时,金额( int amount = 0; )不会增加( amount+=150; )。 Im a beginner so help would be appreciated! 我是一个初学者,所以将不胜感激!

And just an extra thing.. if I want to have the game end if the user gets 3 wrong answers, how should I go about including it into my code? 还有一件额外的事情..如果我想在用户得到3个错误答案的情况下结束游戏,我应该如何将其包含在我的代码中?

Thanks! 谢谢!

This is where I call the method in the the main method.. running it 6 times: 这是我在main方法中调用该方法的地方。运行6次:

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

This is the method which I am calling (contains the math problems): 这是我正在调用的方法(包含数学问题):

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;
   }

First off, this calls the functions 7 times, not 6: 首先,这将调用函数7次,而不是6次:

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

but more significantly, this function is doing exactly what you describe. 但更重要的是,此功能完全可以满足您的描述。 It presents a question, gets a reply, and then prints "LEVEL ONE COMPLETE", every time it's called, because that's how you've written it. 它提出一个问题,得到答复,然后在每次调用时打印“ LEVEL ONE COMPLETE”,因为这就是您编写的方式。 Also, the variable amount is local to the function, so every time you call the function, it updates, and then the value goes out of scope when the function returns, throwing away what you've just computed. 另外,变量的amount是函数的局部变量,因此每次调用函数时,变量都会更新,然后在函数返回时值超出范围,从而丢弃您刚刚计算出的值。

The "amount" variable, and the "LEVEL COMPLETE" message, both need to be moved outside the function. “ amount”变量和“ LEVEL COMPLETE”消息都需要移出该函数。 How you do that it a matter of choice. 您如何选择它是一个选择问题。 You'll probably want to make the variable amount static as well (depending on the rest of the code you're not showing us). 你可能想使变量amount静态以及(取决于你不向我们展示的代码的其余部分)。

Remove last two lines from findAdd findAdd删除最后两行

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

And invoke them in the place where you invoke findAdd six times: 并在调用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);

Your amount needs to be written outside of the findAdd() method. 您的金额需要写在findAdd()方法之外。

int amount = 0;

The following below also needs to be moved to be shown after your for loop and again not in the findAdd() method. 下面的内容也需要移动,以便在for循环之后显示,再次不在findAdd()方法中。

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

Example: 例:

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