import javax.swing.JOptionPane;
public class MathDrillLoop {//start program
public static void main (String[] args) {//start main
//Create start time
long startTime = System.currentTimeMillis();
//declare string output
String output = "";
//declare Number of questions
final int NUMBER_OF_QUESTIONS = 10;
int score = 0;
for (int count = 0; count < 10; count++) {
JOptionPane.showMessageDialog(null,"Correct count is " + count);
//declare integers
int number1, number2, answer, temp;
}
//declare strings
String str1, str2, answerString, replyString;
do{
//Ask user to choose problem type
String entercodeString = JOptionPane.showInputDialog(
"Choose 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division, or 5 for modulo ");
while (! entercode = ("1")&&
entercodeString!= ("2") &&
entercodeString != ("3") &&
entercodeString != ("4") &&
entercodeString != ("5"));
{
output = "Invalid problem type, Try again ";
JOptionPane.showMessageDialog(null,output);
int opcode = Integer.parseInt(entercodeString);
//Switch statements
switch (opcode) {
//if addition enter first number
case (1) : str1 = JOptionPane.showInputDialog(
"Enter integer value, for example 5:");
number1 = Integer.parseInt(str1);
//Enter second number
str2 = JOptionPane.showInputDialog(
"Enter integer value, for example 7:");
number2 = Integer.parseInt(str2);
answerString = JOptionPane.showInputDialog
("What is " + number1 + " + " + number2 + " ? ");
answer = Integer.parseInt(answerString);
if (number1 + number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);
//increase count by 1
count++;
break;
//if subtaction enter first number
case (2) : str1 = JOptionPane.showInputDialog(
"Enter integer value, for example 5:");
number1 = Integer.parseInt(str1);
//Enter second number
str2 = JOptionPane.showInputDialog(
"Enter integer value, for example 7:");
number2 = Integer.parseInt(str2);
if (number1 < number2) {
temp = number1;
number1 = number2;
number2 = temp;
}
answerString = JOptionPane.showInputDialog
("What is " + number1 + " - " + number2 + " ? ");
answer = Integer.parseInt(answerString);
if (number1 - number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);
//increase count by 1
count++;
break;
// if multiplication enter first number
case (3) : str1 = JOptionPane.showInputDialog(
"Enter integer value, for example 5:");
number1 = Integer.parseInt(str1);
//Enter second number
str2 = JOptionPane.showInputDialog(
"Enter integer value, for example 7:");
number2 = Integer.parseInt(str2);
answerString = JOptionPane.showInputDialog
("What is " + number1 + " * " + number2 + " ? ");
answer = Integer.parseInt(answerString);
if (number1 * number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);
//increase count by 1
count++;
break;
//if division enter first number
case (4) : str1 = JOptionPane.showInputDialog(
"Enter integer value, for example 5:");
number1 = Integer.parseInt(str1);
//Enter second number
str2 = JOptionPane.showInputDialog(
"Enter integer value, for example 7:");
number2 = Integer.parseInt(str2);
answerString = JOptionPane.showInputDialog
("What is " + number1 + " / " + number2 + " ? ");
answer = Integer.parseInt(answerString);
if (number1 / number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);
//increase count by 1
count++;
break;
//if modulo enter first number
case (5) : str1 = JOptionPane.showInputDialog(
"Enter integer value, for example 5:");
number1 = Integer.parseInt(str1);
//Enter second number
str2 = JOptionPane.showInputDialog(
"Enter integer value, for example 7:");
number2 = Integer.parseInt(str2);
answerString = JOptionPane.showInputDialog
("What is " + number1 + " % " + number2 + " ? ");
answer = Integer.parseInt(answerString);
if (number1 % number2 == answer){
replyString = "Good Job";
score++;
}
else
replyString = "Incorrect";
JOptionPane.showMessageDialog(null, replyString);
//increase count by 1
count++;
break;
default : output = "Invalid problem type, Try again ";
JOptionPane.showMessageDialog(null,output);
System.exit(0); }
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
output = ("Correct count is " + count +
"\nTest time is " + testTime / 1000 + " seconds\n" + output);
input = JOptionPane.showInputDialog(null, "Do you want to take the quiz again?", "yes or no");
if(input == "yes")
return;
if(input == "no");
System.exit(0);
}
}