/* This program will randomly generate expressions of the form X + Y = * * Where X and Y are integers in the interval [0,100) * * The expression will be presented to the user and the user will have give an answer. * The program will determine if the answer is correct or incorrect. * The program will repeat as long as the user wants to. * * @author Anthony Larrain */ import javax.swing.JOptionPane; class AdditionTest{ public static void main(String [] args){ int correct = 0, incorrect = 0; int response = JOptionPane.showConfirmDialog( null, "Ready To Test You Arithmetic? ", "Confirmation", JOptionPane.YES_NO_OPTION); while(response == JOptionPane.YES_OPTION){ int operand1 = (int)(Math.random() * 100); int operand2 = (int)(Math.random() * 100); int sum = operand1 + operand2; String display = operand1 + " + " + operand2 + " = "; String input = JOptionPane.showInputDialog(display); if(input == null || input.length() == 0){ System.out.println("Invalid input"); } else{ int answer = Integer.parseInt(input); if(answer == sum){ System.out.println("Correct! :)"); ++correct; }else{ System.out.println("Incorrect! :("); ++incorrect; } System.out.println(display + sum); } response = JOptionPane.showConfirmDialog( null, "Try Another Computation ? ", "Ready To Test You Arithmetic?", JOptionPane.YES_NO_OPTION); } System.out.println("Correct -> " + correct + "\nIncorrect -> " + incorrect); System.exit(0); } }