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: Math Drill

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

    Question Math Drill

    Hello fellow programers...I hope someone can help me. I'm new to java and I have an assignment for extra credit that I could really use. This is my first programing course and this problem has me a bit out of my league.

    Anyway the assignment is "Create math drill program that asks category (+,-,/,*), gives 10 random questions, keeps score, time elapsed, and then asks if you want to do it again. 20 points. For an extra 5 points, include modulus."

    So far, this is my code...
        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);
     
    		}
     
    	}
    I hope someone can not only show me the errors I have made but also give me some pointers.

    I bow down to your expertise.

    Neil
    Last edited by helloworld922; February 3rd, 2011 at 12:25 AM.


  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: Math Drill

    Please use:

    [highlight=java] code [/highlgiht]
    It makes code easier to read.
    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);
     
    }
     
    }

    What errors are you getting?

  3. #3
    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: Math Drill

    while (! entercode = ("1")&&
    entercodeString!= ("2") &&
    entercodeString != ("3") &&
    entercodeString != ("4") &&
    entercodeString != ("5"));



    Also, I think with Strings and other Objects, you use .equals(), not == or the stuff used for primitive types.

    And it might be better if you had some number to exit the do....while loop. If you had some number entered that would exit the program, you could just use a bunch of if...else if...else statments
    inside the do...while loop.

    I'm not sure what you are incrementing with count.

    parseInt the input String above and perhaps have an option that asks to exit.
    However...to do this you'll need to create and initialize another int variable called choice.

     
    int valueEntered = 0;
    int choice = 0;
    while (valueEntered!=6 || choice !=JOptionPane.YES_OPTION)
    {
    String enteredByUser = JOptionPane.showInputDialog(null, "Choose 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division, or 5 for modulo ", "Make a choice", JOptionPane.PLAIN_MESSAGE);
    valueEntered = Integer.parseInt(enteredByUser);
     
    // a bunch of if and else if and else statements that will be defined according to what you had in your code to do when those options were selected
     
    // if choice = 6, go to dialog window shown below.
    // if choice is not any of them...say "Error" or something like that,  and...if set up right...it'll just ask again.  
    }

    For how to make the JOptionPane window ask you or no, use this site for reference:

    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    And here might help too:
    http://download.oracle.com/javase/tu...ialogDemo.java
    Just do something like:
    if (choice == JOptionPane.YES_OPTION)
    {
    System.exit(0);
    }
     
    else 
    {
    // don't do anything
    }

    Also, it appears you're defining some values inside that for loop. Define them before it or else they'll go out of scope when it ends.
    Last edited by javapenguin; February 2nd, 2011 at 11:32 PM.

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

    Default Re: Math Drill

    Quote Originally Posted by javapenguin View Post
    Please use:

    [highlight=java] code [/highlgiht]
    It makes code easier to read.
    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);
     
    }
     
    }

    What errors are you getting?
    The code will not compile. It says..." reached end of file while parsing"

    By the way, thanks a lot for helping me out. My teacher speaks broken English, and most of the time I can't understand him!

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Math Drill

    Firstly, welcome to the forums.
    The reality is that your code is filled with logical and syntactical errors and you will have a lot of reading ahead of you.

    • Reached end of file while parsing usually entails a missing brace, where you have opened a curly brace but failed to close it.
    • If you declare variables inside a for-loop, the scope/lifetime of that variable is only going to be inside that loop.
    • Do...while code should be executed inside the bracers after do and is therefore different to a while loop.
    • Like javaPenguin said, to compare String equality, you should use .equals() or .equalsIgnoreCase() and not == or !=, otherwise you are just checking to see if the two Strings share the same memory reference.


    Suggested Reading (Links):
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: Math Drill

    Thanks for the tips everyone. I have a working code kinda. There are still some logical bugs to fix. At the end of the program I am supposed to give the user a chance to either start over or quit. Right now it is not going to the top of the program but it's show the score from the last run.

    Here is the new code. Any help would be great!

       import javax.swing.JOptionPane;
       public class MathDrill{
          public static void main(String[] args){
             final int NUMBER_OF_QUESTIONS = 10;
             int correctCount = 0;
             int count = 0;
             long startTime = System.currentTimeMillis();
             String output ="";
             int option = 0;
     
             do{
     
                JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");                      
     
                int operator = 0;              
     
                //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)");
                operator = Integer.parseInt(operatorString);
     
                while(count < NUMBER_OF_QUESTIONS){
                   int timesleft = (NUMBER_OF_QUESTIONS - count);
                   JOptionPane.showMessageDialog(null, "Question number  "
                      + (NUMBER_OF_QUESTIONS - count));
     
                	//generate two integers
                   int num1 = (int)(Math.random() * 10);
                   int num2 = (int)(Math.random() * 10); 
     
                      if (num1 < num2){
                         int temp = num1;
                         num1 = num2;
                         num2 = temp;
                      }
     
     
                   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)||(operator > 5)){
                         JOptionPane.showMessageDialog(null, "Please reenter number between 1 and 5");
                      }
                   }
     
     
     
                   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++;
                }
     
     
                long endTime = System.currentTimeMillis();
                long testTime = (endTime - startTime);
     
                JOptionPane.showMessageDialog(null, " Correct answers = " + correctCount +
                   "\nYour test took " + testTime /1000 + " seconds\n " + output);
     
     
                option = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
     
             }while(option == JOptionPane.YES_OPTION);
             System.exit(0);
     
          }
     
       }

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

    Default Re: Math Drill

    OK, so I've got a working program now. All I need to figure out is what to do when the random numbers generate a (zero/0) during division and modulo problems. I keep getting errors when that happens. I don't think my teacher would say anything about it but I'm here to learn as much as I can!

    The error reads "Exception in thread "main" java.lang.ArithmeticException: / by zero"

    Once again thanks for everyone's help, and well wishes. I think I will be spending a lot of time here and hopefully learn enough to help others as well.
    Last edited by Java Neil; February 3rd, 2011 at 09:59 PM.

  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: Math Drill

    int x = 0;
    int y = 0;
     
    if (choice = divide)
    {
    // input int 1 like normal
    boolean secondValueIsZero = true;
     
    while (secondValueIsZero==true)
    {
    try
    {
     
    // ask for int2  value;
    secondValueIsZero = false;
    }
     
    catch (ArithmeticException aeRef)
    {
    JOptionPane.showMessageDialog(null, "Cannot divide by zero!" , "Error: / 0 ", JOptionPane.ERROR_MESSAGE);
    }
    }

    The try block checks too see if that exception will occur. If it does, the catch block causes that message to pop up.
    If it doesn't, then the while loop exists.

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

    Default Re: Math Drill

    Quote Originally Posted by javapenguin View Post
    int x = 0;
    int y = 0;
     
    if (choice = divide)
    {
    // input int 1 like normal
    boolean secondValueIsZero = true;
     
    while (secondValueIsZero==true)
    {
    try
    {
     
    // ask for int2  value;
    secondValueIsZero = false;
    }
     
    catch (ArithmeticException aeRef)
    {
    JOptionPane.showMessageDialog(null, "Cannot divide by zero!" , "Error: / 0 ", JOptionPane.ERROR_MESSAGE);
    }
    }

    The try block checks too see if that exception will occur. If it does, the catch block causes that message to pop up.
    If it doesn't, then the while loop exists.
    Sorry about my ignorance, but how would I apply this method? Where would I place the code to catch the error?

  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: Math Drill

    The try block looks for error. Put the thing that might cause the error in there.

    The catch blocks will attempt to point out the error and display the error message.

    Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)

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

    Default Re: Math Drill

    Quote Originally Posted by javapenguin View Post
    The try block looks for error. Put the thing that might cause the error in there.

    The catch blocks will attempt to point out the error and display the error message.

    Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)
    I'll give it a try and report back...Thanks for the heads up. In this case the division and the modulo statements? Right?

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

    Default Re: Math Drill

    After thinking about it for a while, I came up with this...

     
                    if (num1 == 0)
                      num1++;
     
                   if (num2 == 0)
                      num2++;
    Last edited by Java Neil; February 5th, 2011 at 12:17 AM.

Similar Threads

  1. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM
  2. Help need on math java program
    By zidangus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 6th, 2010, 07:41 PM
  3. i can run my program but the math doesnt come otu
    By pairenoid in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2010, 01:39 PM
  4. Question on my math
    By SwEeTAcTioN in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2009, 05:42 PM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM