簡體   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