简体   繁体   中英

Repeating Methods

My program isn't finished yet but I need help in finding a way to run my method 6 times. 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. But whenever I run it, It outputs "LEVEL ONE COMPLETE" at the end of every question. Also, the amount of money ( int amount = 0; ) does not increment ( amount+=150; ) each time the user gets the question right. 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?

Thanks!

This is where I call the method in the the main method.. running it 6 times:

  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:

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. 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.

The "amount" variable, and the "LEVEL COMPLETE" message, both need to be moved outside the function. 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).

Remove last two lines from 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:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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