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