Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: BigMath Method / Allmost there but...

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default BigMath Method / Allmost there but...

    Well I've been working on this one for about a week and I'm at a point where I think I might need some help. We have a assignment called BigMathDrill which is supposed to give the user a choice of (+ - * / %) as a quiz, allow the user to enter the two numbers, grade each problem and return the results either when the user quits, or starts over, plus keep time while using methods for each operation. Whew, that took a lot just to write. Anyway, I've gotten all of that working except the keeping score part. My methods are not returning the input of the user, thus is not being graded by the cases.

    Any help finding what I am doing wrong here would be great!

    Here is my code...

       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 count = 0;
             int num1 = 0;
             int num2 = 0;
             int answer = 0;
             int option = 0;
             int data = 0;
     
             JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill"); 
     
             do {	
     
                do{
                   //Prompt the user to choose a type of quiz
                   String 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)");	
     
                   //Start Question count
                   int timesleft = (NUMBER_OF_QUESTIONS - count);
                   JOptionPane.showMessageDialog(null, "Number of questions  " + 
                   (NUMBER_OF_QUESTIONS - count));
     
                	//Initialize data string	
                   data = Integer.parseInt(drillString);
     
                	//Prevent invalid Operator	
                   if ((data < 1)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                      continue;					
                   }
     
                	//User input for first integer   	            	               
                   String num1String = JOptionPane.showInputDialog("Enter first number");
                   num1 = Integer.parseInt(num1String);
     
                   do{//User input for second integer & prevent division by 0
                      String num2String = JOptionPane.showInputDialog("Enter second integer");
                      num2 = Integer.parseInt(num2String);
     
                      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: add (num1, num2);
                            output += "\n" + num1 + " + " + num2 + " = " + answer +
                               ((num1 + num2 == answer) ? " correct" : " wrong");
                            count++;								
                            break;
     
                         case 2: sub (num1, num2);
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");
                            count++;								
                            break;
     
                         case 3: mult (num1, num2);
                            output += "\n" + num1 + " * " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");
                            count++;
                            break;
     
                         case 4: div (num1, num2);
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");
                            count++;	
                            break;                 
     
                         case 5: mod(num1, num2);
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");
                            count++;	
                            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);
     
                JOptionPane.showMessageDialog(null, " Correct answers = " + 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;
     
             String addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
             answer = Integer.parseInt(addString);		
     
             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;   	   	
          }			
     
       }

    Feel free to judge my code as much as you want. I'm here to learn!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: BigMath Method / Allmost there but...

    Funny. It works pretty good for me.

    It says number of questions 2 whenever I put something.

    If I tell it cancel, it says number of questions 1 and throws a Number Format Exception.

  3. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BigMath Method / Allmost there but...

    Quote Originally Posted by javapenguin View Post
    Funny. It works pretty good for me.

    It says number of questions 2 whenever I put something.

    If I tell it cancel, it says number of questions 1 and throws a Number Format Exception.
    The score I'm referring to is when you end the two questions look at the grade of each question. It will say wrong even if you got it right?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: BigMath Method / Allmost there but...

    I did notice that if you tell it you don't want to play again, that it'll say both are correct.

    Maybe it has something to do with the play again.

    Also, if you get both wrong and tell it not to play again, it'll say wrong, correct.

    If you got both correct but say cancel, it'll say both are wrong.
    Last edited by javapenguin; February 18th, 2011 at 12:38 PM.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: BigMath Method / Allmost there but...

    Hmmmmm....it's also posting number+otherNumber = 0.

    Also your counter starts at 0 when you try again.

    I think it only gives you one try the second time and two tires after that and you counter goes into negatives.

    Find part of your problem though.

    You are printing the answer value, which is still 0 in your main method. Your methods return a value called answer.

    You set it equal to that.

    Now I think I see what's going on.

     switch (data){  
                         case 1: add (num1, num2);
                            output += "\n" + num1 + " + " + num2 + " = " + answer +
                               ((num1 + num2 == answer) ? " correct" : " wrong");
                            count++;                               
                            break;
     
                         case 2: sub (num1, num2);
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");
                            count++;                               
                            break;
     
                         case 3: mult (num1, num2);
                            output += "\n" + num1 + " * " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");
                            count++;
                            break;
     
                         case 4: div (num1, num2);
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");
                            count++;   
                            break;                
     
                         case 5: mod(num1, num2);
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");
                            count++;   
                            break;

    Ok, I got it now.

    You're calling add, but you have to do something with that value.

    Like

    answer = add(num1,num2);

    I'm going to try that and see what happens.
    Last edited by javapenguin; February 18th, 2011 at 12:48 PM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: BigMath Method / Allmost there but...

    Yep, it worked.



    However, it only lets you go 1 question if you play again, your counter goes into negatives, and it never clears it so that your JOptionPane could be filled up if they keep playing again many times.



    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 count = 0;
             int num1 = 0;
             int num2 = 0;
             int answer = 0;
             int option = 0;
             int data = 0;
     
     
     
             JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");
     
             do {  
     
                do{
                   //Prompt the user to choose a type of quiz
                   String 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)");   
     
                   //Start Question count
                   int timesleft = (NUMBER_OF_QUESTIONS - count);
                   JOptionPane.showMessageDialog(null, "Number of questions  " +
                   (NUMBER_OF_QUESTIONS - count));
     
                    //Initialize data string   
                   data = Integer.parseInt(drillString);
     
                    //Prevent invalid Operator 
                   if ((data < 1)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                      continue;                
                   }
     
                    //User input for first integer                                     
                   String num1String = JOptionPane.showInputDialog("Enter first number");
                   num1 = Integer.parseInt(num1String);
     
                   do{//User input for second integer & prevent division by 0
                      String num2String = JOptionPane.showInputDialog("Enter second integer");
                      num2 = Integer.parseInt(num2String);
     
                      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++;                               
                            break;
     
                         case 2: 
    							answer = sub (num1, num2);
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");
                            count++;                               
                            break;
     
                         case 3:
    							answer = mult (num1, num2);
                            output += "\n" + num1 + " * " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");
                            count++;
                            break;
     
                         case 4: 
    							answer = div (num1, num2);
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");
                            count++;   
                            break;                
     
                         case 5:
    							answer =  mod(num1, num2);
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");
                            count++;   
                            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, " Correct answers = " + 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;
     
             String addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
             answer = Integer.parseInt(addString);     
     
             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;        
          }        
     
       }
    Last edited by javapenguin; February 18th, 2011 at 01:41 PM.

  7. #7
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BigMath Method / Allmost there but...

    Good call! You are the man, and I just learned a very important lesson about methods and cases! Thanks for opening my eyes to that one!

    I fixed the counter...sorta? It no longer goes into the negative. I put the ( int count = 0; ) in the first do - while loop. Now the only problem I have is getting it to remember that last count and keep up with the total questions asked until the user exits.

    Here is what I have...

       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;
     
     
             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  " +
                      (NUMBER_OF_QUESTIONS - count)); 
     
                do{
                   //Prompt the user to choose a type of quiz
                   String 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);
     
                    //Prevent invalid Operator
                   if ((data < 1)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                      continue;                
                   }
     
                    //User input for first integer                                    
                   String num1String = JOptionPane.showInputDialog("Enter first number");
                   num1 = Integer.parseInt(num1String);
     
                   do{//User input for second integer & prevent division by 0
                      String num2String = JOptionPane.showInputDialog("Enter second integer");
                      num2 = Integer.parseInt(num2String);
     
                      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++;                              
                            break;
     
                         case 2:
                            answer = sub (num1, num2);
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");
                            count++;                              
                            break;
     
                         case 3:
                            answer = mult (num1, num2);
                            output += "\n" + num1 + " * " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");
                            count++;
                            break;
     
                         case 4:
                            answer = div (num1, num2);
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");
                            count++;  
                            break;                
     
                         case 5:
                            answer =  mod(num1, num2);
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");
                            count++;  
                            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;
     
             String addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
             answer = Integer.parseInt(addString);    
     
             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;        
          }        
     
       }

    I'm stating to see the big picture with Java. I'm only starting out but I can see the potential here.

    Neil

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: BigMath Method / Allmost there but...

    Well, I see that if I define count outside both of the do while llops it will counter the cumalitive number of questions correctly, but will only allow 1 try and will go into negative counter.

  9. #9
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BigMath Method / Allmost there but...

    Quote Originally Posted by javapenguin View Post
    Well, I see that if I define count outside both of the do while llops it will counter the cumalitive number of questions correctly, but will only allow 1 try and will go into negative counter.
    Yep, I came up with the same thing...there must be a way to save the memory address from each attempt. Some type of count++ or something?

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: BigMath Method / Allmost there but...

    I was able to fix it slightly.

    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;        
          }        
     
       }

    Now it'll say question1 for 1st question, question 3 for third question, question 5 for 5th question, etc.

  11. #11
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BigMath Method / Allmost there but...

    Well I'm almost there...I have gotten the code to do everything except keep adding the totals for the number of questions the user has gotten wrong or right. My problem is when the user chooses to either take the quiz again, it re-initializes the "count' which of coarse defaults back to 0. I'm sure there is a way to "return" the count as to keep it in memory, but not effect the number of questions, but for the life of me I can figure it out. The problem is that for this assignment I need to use methods to implement everything so I can't just write the code straight forward.

    Any insight?

    Here is the code I have so far...

       import javax.swing.JOptionPane;
       public class UserMathDrill {  
          public static void main(String[] args) {
             //Declare almost everything
             String output = "";
             long startTime = System.currentTimeMillis();          
             final int NUMBER_OF_QUESTIONS = 5;
             int num1 = 0;
             int num2 = 0;
             int answer = 0;
             int option = 0;
             int data = 0;
     
     
             JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");
     
             do {  
     
                int count = 0;
                int negcount = 0;
     
                do {
                   //Prompt the user to choose a type of quiz
                   String 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)");  
     
                   //Start Question count
                   int timesleft = (NUMBER_OF_QUESTIONS - (count + negcount));
     
                   //Initialize data string  
                   data = Integer.parseInt(drillString);
     
                   //Prevent invalid Operator
                   if ((data < 1)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                      continue;                
                   }
                   JOptionPane.showMessageDialog(null, "Question number  " +
                      (NUMBER_OF_QUESTIONS - (count + negcount) + " of 5")); 
     
                   //User input for first integer                                    
                   String num1String = JOptionPane.showInputDialog("Enter first number");
                   num1 = Integer.parseInt(num1String);
     
                   do {
     
                      //User input for second integer & prevent division by 0
                      String num2String = JOptionPane.showInputDialog("Enter second integer");
                      num2 = Integer.parseInt(num2String);
     
                      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){
     
                         //Addition  
                         case 1: answer =  add (num1, num2);
     
                            if (num1 + num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " + " + num2 + " = " + answer +
                                  ((num1 + num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " + " + num2 + " = " + (num1 + num2));
                            negcount++;
     
                            output += "\n" + num1 + " + " + num2 + " = " + answer +
                               ((num1 + num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Subtraction                   
                         case 2: answer = sub (num1, num2);
     
                            if (num1 - num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " - " + num2 + " = " + answer +
                                  ((num1 - num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " - " + num2 + " = " + (num1 - num2));
                            negcount++;
     
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Multiplication
                         case 3: answer = mult (num1, num2);
     
                            if (num1 * num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " * " + num2 + " = " + answer +
                                  ((num1 * num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " * " + num2 + " = " + (num1 * num2));
                            negcount++;
     
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Division
                         case 4: answer = div (num1, num2);
     
                            if (num1 / num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " / " + num2 + " = " + answer +
                                  ((num1 * num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " * " + num2 + " = " + (num1 / num2));
                            negcount++;
     
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");                              
                            break;                
     
                      	//Modulo                                         
                         case 5: answer =  mod(num1, num2);
     
                            if (num1 % num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " % " + num2 + " = " + answer +
                                  ((num1 % num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " % " + num2 + " = " + (num1 % num2));
                            negcount++;
     
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");                              
                            break;                                                                                 
                      }
     
                   } while((data != 1) && (data != 2) && (data != 3) && (data != 4) && (data != 5));  
     
                } while (count + negcount < 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, "Right Answers = " + count + 
    				"\nWrong Answers = " + negcount + "\nYour test took " + testTime /1000 + 
    				" seconds\n " + "\n<html><u><i><b>Total score card</b></i></u></html>" + output);         
     
             }while (option == JOptionPane.YES_OPTION);
             JOptionPane.showMessageDialog(null, "Good Bye");
     
          }
     
          //Addition Quiz
          public static int add(int num1, int num2) {
     
             int answer = 0;
     
             String addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
             answer = Integer.parseInt(addString);    
     
             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);
     
             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);
     
             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);
     
             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);
     
             return answer;        
          }        
     
       }

    What do you think?

    Neil

  12. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool This looks like it works now

    It took a while but I think this does it, though you'll have to check to make sure.



    This fixes that error and also the error that kept making your division answers usually wrong. (You had it checking num*num2 for division)

     import javax.swing.JOptionPane;
       public class UserMathDrill {  
          public static void main(String[] args) {
             //Declare almost everything
             String output = "";
             long startTime = System.currentTimeMillis();          
             final int NUMBER_OF_QUESTIONS = 5;
             int num1 = 0;
             int num2 = 0;
             int answer = 0;
             int option = 0;
             int data = 0;
    			int right = 0;
    			int wrong = 0;
     
             JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");
     
             do {  
     
              int count = 0;
                int negcount = 0;
     
     
                do {
                   //Prompt the user to choose a type of quiz
                   String 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)");  
     
                   //Start Question count
                   int timesleft = (NUMBER_OF_QUESTIONS - (count + negcount));
     
                   //Initialize data string  
                   data = Integer.parseInt(drillString);
     
                   //Prevent invalid Operator
                   if ((data < 1)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                      continue;                
                   }
                   JOptionPane.showMessageDialog(null, "Question number  " +
                      (NUMBER_OF_QUESTIONS - (count + negcount) + " of 5")); 
     
                   //User input for first integer                                    
                   String num1String = JOptionPane.showInputDialog("Enter first number");
                   num1 = Integer.parseInt(num1String);
     
                   do {
     
                      //User input for second integer & prevent division by 0
                      String num2String = JOptionPane.showInputDialog("Enter second integer");
                      num2 = Integer.parseInt(num2String);
     
                      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){
     
                         //Addition  
                         case 1: answer =  add (num1, num2);
     
                            if (num1 + num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " + " + num2 + " = " + answer +
                                  ((num1 + num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " + " + num2 + " = " + (num1 + num2));
                            negcount++;
     
                            output += "\n" + num1 + " + " + num2 + " = " + answer +
                               ((num1 + num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Subtraction                   
                         case 2: answer = sub (num1, num2);
     
                            if (num1 - num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " - " + num2 + " = " + answer +
                                  ((num1 - num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " - " + num2 + " = " + (num1 - num2));
                            negcount++;
     
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 - num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Multiplication
                         case 3: answer = mult (num1, num2);
     
                            if (num1 * num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " * " + num2 + " = " + answer +
                                  ((num1 * num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " * " + num2 + " = " + (num1 * num2));
                            negcount++;
     
                            output += "\n" + num1 + " - " + num2 + " = " + answer +
                               ((num1 * num2 == answer) ? " correct" : " wrong");                              
                            break;
     
                      	//Division
                         case 4: answer = div (num1, num2);
     
                            if (num1 / num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " / " + num2 + " = " + answer +
                                  ((num1 / num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " / " + num2 + " = " + (num1 / num2));
                            negcount++;
     
                            output += "\n" + num1 + " / " + num2 + " = " + answer +
                               ((num1 / num2 == answer) ? " correct" : " wrong");                              
                            break;                
     
                      	//Modulo                                         
                         case 5: answer =  mod(num1, num2);
     
                            if (num1 % num2 == answer){
                               JOptionPane.showMessageDialog(null, "You are Correct");
                               count++;
     
                               output += "\n" + num1 + " % " + num2 + " = " + answer +
                                  ((num1 % num2 == answer) ? " correct" : " wrong"); 
                               break;
                            }
                            else
                               JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                                  + num1 + " % " + num2 + " = " + (num1 % num2));
                            negcount++;
     
                            output += "\n" + num1 + " % " + num2 + " = " + answer +
                               ((num1 % num2 == answer) ? " correct" : " wrong");                              
                            break;                                                                                 
                      }
     
                   } while((data != 1) && (data != 2) && (data != 3) && (data != 4) && (data != 5));  
     
                } while (count + negcount < 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);
    				right = right + count;
    				wrong = wrong + negcount;
                JOptionPane.showMessageDialog(null, "Right Answers = " + right + 
    				"\nWrong Answers = " + wrong + "\nYour test took " + testTime /1000 + 
    				" seconds\n " + "\n<html><u><i><b>Total score card</b></i></u></html>" + output);         
     
             }while (option == JOptionPane.YES_OPTION);
             JOptionPane.showMessageDialog(null, "Good Bye");
     
          }
     
     
          //Addition Quiz
          public static int add(int num1, int num2) {
     
             int answer = 0;
     
             String addString = JOptionPane.showInputDialog ("What is " + num1 + " + " + num2 + "? ");
             answer = Integer.parseInt(addString);    
     
             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);
     
             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);
     
             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);
     
             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);
     
             return answer;        
          }        
     
       }

Similar Threads

  1. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  2. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM