Hi all...I'm in a pickle with an assignment and I hope someone can help me sort this out. I have a program called MathDrill that I have used case statements for the operators. Now my teacher wants me to switch these case statements to method calls. Can someone explain what he means (he speaks broken English) and maybe help me a bit with my code, at least to get me going in the right direction?
Here's what I have...
import javax.swing.JOptionPane; public class MathDrillMethod{ public static void main(String[] args){ final int NUMBER_OF_QUESTIONS = 2; int correctCount = 0; long startTime = System.currentTimeMillis(); String output =""; int option = 0; do{ JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill"); //prompt user to enter a math operator for test String operatorString = JOptionPane.showInputDialog(null, "<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)"); int operator = 0; operator = Integer.parseInt(operatorString); if ((operator < 1)||(operator > 5)){ JOptionPane.showMessageDialog(null, "Error: Invalid Operator"); System.exit(0); } //initialize count int count = 0; while(count < NUMBER_OF_QUESTIONS){ int timesleft = (NUMBER_OF_QUESTIONS - count); JOptionPane.showMessageDialog(null, "Question number " + (NUMBER_OF_QUESTIONS - count)); //generate two integers that work with everything int num1 = (int)(Math.random() * 10); int num2 = (int)(Math.random() * 10); if (num1 == 0) num1++; if (num2 == 0) num2++; if (num1 < num2){ int temp = num1; num1 = num2; num2 = temp; while ((num2 == 0) && ((operator != 4)||(operator != 5))){ } } while ((operator < 1) || (operator > 5)){ char symbol; switch (operator){ case 1: symbol = '+'; break; case 2: symbol = '-'; break; case 3: symbol = '*'; break; case 4: symbol = '/'; break; case 5: symbol = '%'; break; } } if (operator == 1){ String answerString = JOptionPane.showInputDialog(null, " What is " + num1 + " + " + num2 + "?"); int answer = Integer.parseInt(answerString); //grade results if (num1 + num2 == answer){ JOptionPane.showMessageDialog(null, "You are Correct"); correctCount++; } else JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be " + num1 + " + " + num2 + " = " + (num1 + num2)); output += "\n" + num1 + " + " + num2 + " = " + answer + ((num1 + num2 == answer) ? " correct" : " wrong"); } if (operator == 2){ String answerString = JOptionPane.showInputDialog(null, " What is " + num1 + " - " + num2 + " ? "); int answer = Integer.parseInt(answerString); //grade results if (num1 - num2 == answer){ JOptionPane.showMessageDialog(null, "You are Correct"); correctCount++; } else JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be " + num1 + " - " + num2 + " = " + (num1 - num2)); output += "\n" + num1 + " - " + num2 + " = " + answer + ((num1 - num2 == answer) ? " correct" : " wrong"); } if (operator == 3){ String answerString = JOptionPane.showInputDialog(null, " What is " + num1 + " * " + num2 + " ? "); int answer = Integer.parseInt(answerString); //grade results if (num1 * num2 == answer){ JOptionPane.showMessageDialog(null, " You are Correct"); correctCount++; } else JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be " + num1 + " * " + num2 + " = " + (num1 * num2)); output += "\n" + num1 + " * " + num2 + " = " + answer + ((num1 * num2 == answer) ? " correct" : " wrong"); } if (operator == 4){ String answerString = JOptionPane.showInputDialog(null, " What is " + num1 + " / " + num2 + " ? "); int answer = Integer.parseInt(answerString); //grade results if (num1 / num2 == answer){ JOptionPane.showMessageDialog(null, " You are Correct"); correctCount++; } else JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be " + num1 + " / " + num2 + " = " + (num1 / num2)); output += "\n" + num1 + " / " + num2 + " = " + answer + ((num1 / num2 == answer) ? " correct" : " wrong"); } if (operator == 5){ String answerString = JOptionPane.showInputDialog(null, " What is " + num1 + " % " + num2 + " ? "); int answer = Integer.parseInt(answerString); //grade results if (num1 % num2 == answer){ JOptionPane.showMessageDialog(null," You are Correct "); correctCount++; } else JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be " + num1 + " % " + num2 + " = " + (num1 % num2)); output += "\n" + num1 + " % " + num2 + " = " + answer + ((num1 % num2 == answer) ? " correct" : " wrong"); } count++; } option = JOptionPane.showConfirmDialog(null, "Do you want to play again?"); long endTime = System.currentTimeMillis(); long testTime = (endTime - startTime); JOptionPane.showMessageDialog(null, " Correct answers = " + correctCount + "\nYour test took " + testTime /1000 + " seconds\n " + output); }while(option == JOptionPane.YES_OPTION); System.exit(0); } }
Thanks in advance for your help!