import javax.swing.JOptionPane;
public class inputmathquizx {
public static void main(String[] args) {
bigMethod();
}
public static void bigMethod() {
//big declare
long time = 0;
final int NUMBER_OF_QUESTIONS = 5;
int input1 = 0;
int input2 = 0;
int answer = 0;
int option = 0;
int data = 0;
int count = 0;
int wrong = 0;
String output = "";
do {
//re declare
time = System.currentTimeMillis();
wrong = 0;
count = 0;
output = "";
do{
//choose a quiz
String operaterString = JOptionPane.showInputDialog(null,
"Choose a quiz.\n Addtion: 1\n" +
"Subtraction: 2\n Multiplication: 3\n Division: 4\n Modulus: 5\n");
//data string
data = Integer.parseInt(operaterString);
//invalid Operator
if ((data <= 0)||(data > 5)){
JOptionPane.showMessageDialog(null, "Invalid Operator!");
continue;
}
// count number of questions left
int timesleft = (NUMBER_OF_QUESTIONS - (count + wrong));
if(timesleft == 1){
JOptionPane.showMessageDialog(null, "You can do " +
(NUMBER_OF_QUESTIONS - (count + wrong)) + " more question.");
}
else
JOptionPane.showMessageDialog(null, "You can do " +
(NUMBER_OF_QUESTIONS - (count + wrong)) + " more questions.");
//User input for the 1st number
do{String number1String = JOptionPane.showInputDialog("Enter first number");
input1 = Integer.parseInt(number1String);
do{//User input for the 2nd number
String number2String = JOptionPane.showInputDialog("Enter second number");
input2 = Integer.parseInt(number2String);
// cant divide by zero
if ((input2 == 0) && (data == 4) || (input2 == 0) && (data == 5)){
JOptionPane.showMessageDialog(null, "you can't divide by zero.");
}
// cant subtract from zero
else if((input1 == 0) && (data == 2)) {
JOptionPane.showMessageDialog(null, "you can't subtract from zero.");
}
}while((data == 4) && (input2 == 0) || (data == 5) && (input2 == 0));
}while((input1 == 0) && (data == 2));
do {
//Cases
switch (data){
case 1: answer = addition(input1, input2);
if (answer == input1 + input2){
output += "\n" + input1 + " + " + input2 + " = " + answer +
((input1 + input2 == answer) ? " correct" : " wrong");
count++;
}
else{
output += "\n" + input1 + " + " + input2 + " = " + answer +
((input1 + input2 == answer) ? " correct" : " wrong");
wrong++;
}
break;
case 2: answer = subtraction (input1, input2);
if (answer == input1 - input2){
output += "\n" + input1 + " - " + input2 + " = " + answer +
((input1 - input2 == answer) ? " correct" : " wrong");
count++;
}
else{
output += "\n" + input1 + " - " + input2 + " = " + answer +
((input1 - input2 == answer) ? " correct" : " wrong");
wrong++;
}
break;
case 3: answer = multiply (input1, input2);
if (answer == input1 * input2){
output += "\n" + input1 + " - " + input2 + " = " + answer +
((input1 * input2 == answer) ? " correct" : " wrong");
count++;
}
else{
output += "\n" + input1 + " * " + input2 + " = " + answer +
((input1 * input2 == answer) ? " correct" : " wrong");
wrong++;
}
break;
case 4: answer = divide (input1, input2);
if (answer == input1 / input2){
output += "\n" + input1 + " / " + input2 + " = " + answer +
((input1 / input2 == answer) ? " correct" : " wrong");
count++;
}
else{
output += "\n" + input1 + " / " + input2 + " = " + answer +
((input1 / input2 == answer) ? " correct" : " wrong");
wrong++;
}
break;
case 5: answer = modulus (input1, input2);
if (answer == input1 % input2){
output += "\n" + input1 + " % " + input2 + " = " + answer +
((input1 % input2 == answer) ? " correct" : " wrong");
count++;
}
else{
output += "\n" + input1 + " % " + input2 + " = " + answer +
((input1 % input2 == answer) ? " correct" : " wrong");
wrong++;
}
break;
}
} while((data != 1) && (data != 2) && (data != 3) && (data != 4) && (data != 5));
} while (count + wrong < NUMBER_OF_QUESTIONS);
//time the quiz took
long endTime = System.currentTimeMillis();
long testTime = (endTime - time);
//repeat quiz
option = JOptionPane.showConfirmDialog(null, "Would you like to take another quiz?");
//GUI to show right and wrong answer
JOptionPane.showMessageDialog(null, " Correct answers = " + count + "\nwrong answers =" +wrong+
"\nIt took you " + testTime /1000 + " seconds to complete this quiz\n " + output);
}while (option == JOptionPane.YES_OPTION);
}
//add
public static int addition(int input1, int input2) {
int answer = 0;
String aString = JOptionPane.showInputDialog ("What is " + input1 + " + " + input2 + "? ");
answer = Integer.parseInt(aString);
if (input1 + input2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " + " +
input2 + " should be " + (input1 + input2));
return answer;
}
//sub
public static int subtraction(int input1, int input2) {
int answer = 0;
String sString = JOptionPane.showInputDialog ("What is " + input1 + " - " + input2 + "? ");
answer = Integer.parseInt(sString);
if (input1 - input2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " - " +
input2 + " should be " + (input1 - input2));
return answer;
}
//mult
public static int multiply(int input1, int input2) {
int answer = 0;
String mString = JOptionPane.showInputDialog ("What is " + input1 + " x " + input2 + "? ");
answer = Integer.parseInt(mString);
if (input1 * input2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " x " +
input2 + " should be " + (input1 * input2));
return answer;
}
//divide
public static int divide(int input1, int input2) {
int answer = 0;
String dString = JOptionPane.showInputDialog ("What is " + input1 + " / " + input2 + "? ");
answer = Integer.parseInt(dString);
if (input1 / input2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " / " +
input2 + " should be " + (input1 / input2));
return answer;
}
//mod
public static int modulus(int input1, int input2) {
int answer = 0;
String moduloString = JOptionPane.showInputDialog ("What is " + input1 + " % " + input2 + "? ");
answer = Integer.parseInt(moduloString);
if (input1 % input2 == answer){
JOptionPane.showMessageDialog(null, "You are correct!");
}
else
JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " % " +
input2 + " should be " + (input1 % input2));
return answer;
}
}