私のプログラムはまだ完成していませんが、メソッドを 6 回実行する方法を見つけるのに助けが必要です。問題の出力と答えの計算をメソッドで行う算数ドリルゲームです。理想的には、メソッドを 6 回 (合計で 6 つの数学の問題) 実行した後、"LEVEL ONE COMPLETE" ステートメントを出力します。しかし、実行するたびに、すべての質問の最後に「LEVEL ONE COMPLETE」が出力されます。また、ユーザーが問題を正解するたびに金額 ( int amount = 0;
) が増加 ( ) するわけではありません。amount+=150;
私は初心者なので、助けていただければ幸いです!
そして、余分なこと..ユーザーが3つの間違った答えを取得した場合にゲームを終了させたい場合、それをコードに含めるにはどうすればよいですか?
ありがとう!
これは、メイン メソッドでメソッドを呼び出す場所です。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;
}