My program is a math game which consists of different levels and different math operations. I'm not completely finished yet but I would like to know if there is a way to put the code for each of the four operators into their own method, and just have the main method print the questions and ask for the answers? Also, why when I run my program, when the user enters their answer, does the pop up close and make me enter it in the program (not in a window)?

import javax.swing.*;
import java.io.*;

public class game
{

  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed by the user 

    int money = 0;

    String player = JOptionPane.showInputDialog(null, "Welcome to... \n - Are YOU Smarter Than a 12 Year Old? - \n Please enter your name to begin!", "Welcome", JOptionPane.INFORMATION_MESSAGE);

   JOptionPane.showMessageDialog(null, "Hi " + player + ", " + " let's see if you are really smarter than a 12 year old. \n This games consists of 3 levels of difficulty. \n Answer all 4 questions in each level and earn $500, 000! \n If you get an answer wrong you lose $100, you go home EMPTY HANDED if your money reaches 0!");

   Object[] options = {"Yes!", "No way!"};

   int x = JOptionPane.showOptionDialog(null,"Are you ready to play?","Start?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

if (x == JOptionPane.YES_OPTION) {

   JOptionPane.showMessageDialog(null, "Your first level consists of addition and substraction of 2 numbers! \n In order to pass this level, you will need to answer all 3 questions correctly. \n For every correct question, you will earn $1,000.");

   int addA = leveloneA();
   int addB = leveloneB();
   String sumA = JOptionPane.showInputDialog(null, addA + "+" + addB + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
   sumA = myInput.readLine();
   int sum = Integer.parseInt (sumA);

   int realSum = addA + addB;
   JOptionPane.showMessageDialog(null, realSum);

   if (sum == realSum){

     money = money + 1000;

      JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
   }
      if (sum != realSum){
        JOptionPane.showMessageDialog(null, "INCORRECT! \n You are not smarter than a 12 year old. \n You go home with $" + money);
        System.exit(0);
      }
 }
else if (x == JOptionPane.NO_OPTION){
  JOptionPane.showMessageDialog(null, "Goodbye!");
  System.exit(0);
}

   int subA = leveloneA();
   int subB = leveloneB();

   if (subA < subB){
     subA = subB;
     subB = subA;
   }
   String differenceA = JOptionPane.showInputDialog(null, subA + " - " + subB + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
   differenceA = myInput.readLine();
   int difference = Integer.parseInt (differenceA);

   int realDifference = subA - subB;
   JOptionPane.showMessageDialog(null, realDifference);
   if (difference == realDifference){

     money = money + 1000;

      JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
   }
   else if (difference != realDifference){

     JOptionPane.showMessageDialog(null, "INCORRECT! \n You are not smarter than a 12 year old. \n You go home with $" + money);
     System.exit(0);
   }
  }

  public static int leveloneA ()
  {
    int firstNum = (int)(Math.random()*20);
    return firstNum;
  }
   public static int leveloneB ()
  {
    int secondNum = (int)(Math.random()*20);
    return secondNum;
  }


}

I would like to know if there is a way to put the code for each of the four operators into their own method, and just have the main method print the questions and ask for the answers?

Yes, it's just like you said! Put the code for each operator in its own method, and call those methods from the main method. It really is that simple, just try it.

why when I run my program, when the user enters their answer, does the pop up close and make me enter it in the program (not in a window)?

Because that's how you coded it!

String sumA = JOptionPane.showInputDialog(...  // pop up dialog, get user's input
sumA = myInput.readLine(); // ignore that, and read input from console!

also, having your main method throw Exceptions is a very bad design, you should get out of your system as soon as you can.
remember: the main method is the very last place where you can catch the exceptions and add some exception handling.

@JamesCherrill- thanks I will change it, but is it possible to return more the
an one variable to the main method(firstNum, secondNum, sum)?
@stultuuske- thank you, I will work on that.

A method can only return one value (although that may be an object with many instance variables). In your case it's probably easier to have firstNum etc as class variables so all the methods can share them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.