import javax.swing.JOptionPane;
public class UserMathDrill {
public static void main(String[] args) {
//Declare everything
String output = "";
long startTime = System.currentTimeMillis();
final int NUMBER_OF_QUESTIONS = 2;
int num1 = 0;
int num2 = 0;
int answer = 0;
int option = 0;
int data = 0;
int questionNumber = 1;
JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");
do {
int count = 0;
//Start Question count
int timesleft = (NUMBER_OF_QUESTIONS - count);
JOptionPane.showMessageDialog(null, "Question number " + questionNumber);
do{
//Prompt the user to choose a type of quiz
boolean isAValidInt = false;
String drillString = "";
while (isAValidInt == false)
{
try
{
drillString = JOptionPane.showInputDialog( "<html><u><i><b>Please enter operation</b></i></u></html>"
+ "\nFor addition problems enter (1)" + "\nFor subtraction problems enter (2)" +
"\nFor multiplication problems enter (3)" + "\nFor division problems enter (4)" +
"\nFor modulo problems enter (5)");
//Initialize data string
data = Integer.parseInt(drillString);
isAValidInt = true;
}
catch (NumberFormatException nfeRef)
{
JOptionPane.showMessageDialog(null, "Enter an integer.", "Number Format Excpetion", JOptionPane.ERROR_MESSAGE);
}
}
//Prevent invalid Operator
if ((data < 1)||(data > 5)){
JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
continue;
}
//User input for first integer
boolean isAnInteger2 = false;
String num1String = "";
while (isAnInteger2 == false)
{
try
{
num1String = JOptionPane.showInputDialog("Enter first number");
num1 = Integer.parseInt(num1String);
isAnInteger2 = true;
}
catch (NumberFormatException nfeRef)
{
JOptionPane.showMessageDialog(null, "Enter an integer.", "Number Format Excpetion", JOptionPane.ERROR_MESSAGE);
}
}
do{//User input for second integer & prevent division by 0
String num2String = "";
boolean isAValidInt3 = false;
while (isAValidInt3 == false)
{
try
{
num2String = JOptionPane.showInputDialog("Enter second integer");
num2 = Integer.parseInt(num2String);
isAValidInt3 = true;
}
catch (NumberFormatException nfeRef)
{
JOptionPane.showMessageDialog(null, "Enter an integer.", "Number Format Excpetion", JOptionPane.ERROR_MESSAGE);
}
}
if ((num2 == 0) && (data == 4) || (num2 == 0) && (data == 5))
JOptionPane.showMessageDialog(null, "Cannot divide by zero");
}while((data == 4) && (num2 == 0) || (data == 5) && (num2 == 0));
do {
//Case statements
switch (data){
case 1:
answer = add (num1, num2);
output += "\n" + num1 + " + " + num2 + " = " + answer +
((num1 + num2 == answer) ? " correct" : " wrong");
count++;
questionNumber++;
break;
case 2:
answer = sub (num1, num2);
output += "\n" + num1 + " - " + num2 + " = " + answer +
((num1 - num2 == answer) ? " correct" : " wrong");
count++;
questionNumber++;
break;
case 3:
answer = mult (num1, num2);
output += "\n" + num1 + " * " + num2 + " = " + answer +
((num1 * num2 == answer) ? " correct" : " wrong");
count++;
questionNumber++;
break;
case 4:
answer = div (num1, num2);
output += "\n" + num1 + " / " + num2 + " = " + answer +
((num1 / num2 == answer) ? " correct" : " wrong");
count++;
questionNumber++;
break;
case 5:
answer = mod(num1, num2);
output += "\n" + num1 + " % " + num2 + " = " + answer +
((num1 % num2 == answer) ? " correct" : " wrong");
count++;
questionNumber++;
break;
}
} while((data != 1) && (data != 2) && (data != 3) && (data != 4) && (data != 5));
} while (count < NUMBER_OF_QUESTIONS);
option = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
long endTime = System.currentTimeMillis();
long testTime = (endTime - startTime);
System.out.println(output);
JOptionPane.showMessageDialog(null, " Number of Questions = " + count +
"\nYour test took " + testTime /1000 + " seconds\n " + output);
}while (option == JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "Good Bye");
}
//Addition Quiz
public static int add(int num1, int num2) {
int answer = 0;
boolean isAValidAnswer = false;
String addString = "";
while (isAValidAnswer == false)
{
try
{
addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
answer = Integer.parseInt(addString);
isAValidAnswer = true;
}
catch(NumberFormatException nfeRef)
{
JOptionPane.showMessageDialog(null, "Enter an integer.", "Number Format Excpetion", JOptionPane.ERROR_MESSAGE);
}
}
if (num1 + num2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + num1 + " + " +
num2 + " should be " + (num1 + num2));
return answer;
}
//Subtraction Quiz
public static int sub(int num1, int num2) {
int answer = 0;
String subString = JOptionPane.showInputDialog ("What is " + num1 + " - " + num2 + "? ");
answer = Integer.parseInt(subString);
if (num1 - num2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + num1 + " - " +
num2 + " should be " + (num1 - num2));
return answer;
}
//Multiplication Quiz
public static int mult(int num1, int num2) {
int answer = 0;
String multString = JOptionPane.showInputDialog ("What is " + num1 + " * " + num2 + "? ");
answer = Integer.parseInt(multString);
if (num1 * num2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + num1 + " * " +
num2 + " should be " + (num1 * num2));
return answer;
}
//Division Quiz
public static int div(int num1, int num2) {
int answer = 0;
String dString = JOptionPane.showInputDialog ("What is " + num1 + " / " + num2 + " ? ");
answer = Integer.parseInt(dString);
if (num1 / num2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + num1 + " / " +
num2 + " should be " + (num1 / num2));
return answer;
}
//Modulo Quiz
public static int mod(int num1, int num2) {
int answer = 0;
String modString = JOptionPane.showInputDialog ("What is " + num1 + " % " + num2 + " ? ");
answer = Integer.parseInt(modString);
if (num1 % num2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + num1 + " / " +
num2 + " should be " + (num1 % num2));
return answer;
}
}