1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Rhino's "I need Java help" thread

Discussion in 'Programming & Webmastering' started by Easy Rhino, Sep 10, 2009.

  1. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    hey guys and gals, i am trying to write a java problem that accomplishes a fairly simple task. the program asks if a student would like to solve an addition problem or a subtraction problem. the problem generates 2 random numbers. whichever option the user chooses the program then asks the corresponding question. the user can input their answer and it the program needs to reply back with either correct or incorrect. my main problem is getting the program to recognize the actual math being done in the background and be able to check the answer the user inputs as correct or incorrect. here is my code so far. any help is greatly appreciated.

    import javax.swing.JOptionPane;

    public class elementarymath
    {
    private String inputString;
    public static int response;
    private int simple_math, simple_answer, num1, num2, add_eq, sub_eq;

    public void userChoice()
    {
    inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    simple_math = Integer.parseInt(inputString);
    simple_answer = Integer.parseInt(inputString);
    num1 = RandomNumber();
    num2 = RandomNumber();

    switch(simple_math)
    {
    case 1:
    JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    break;
    case 2:
    JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    break;
    }
    switch(simple_answer)
    {
    case 1:
    elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    break;
    case 2:
    elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    break;
    }
    }
    public int RandomNumber()
    {
    return ((int) (Math.random() * 10)); //generates random number and multiplies by ten
    }
    public static void main(String[] args)
    {
    elementarymath x = new elementarymath();
    do
    {
    x.userChoice();
    }
    while (response == 0);

    System.exit(0);
    }
    }
     
  2. YinYang.ERROR

    YinYang.ERROR New Member

    Joined:
    Apr 7, 2009
    Messages:
    463 (0.22/day)
    Thanks Received:
    33
    I'm guessing this is a simple GUI app, probably for class. As I have really no idea about GUI coding in Java, I'm not sure how much help I would be.

    But I'm guessing your going to need to check if num1 + num2 (or num1 - num2) is equal to the answer the user inputs.

    but again I'm not 100% sure what you are doing with that code. I don't know what exactly this function is doing: Integer.parseInt(inputString);


    Also, what is the purpose of the simple_math and simple_answer variables?
     
  3. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    simple_answer will always equal 1 or 2.


    I think...

    Replace this:
    Code:
    inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    simple_math = Integer.parseInt(inputString);
    simple_answer = Integer.parseInt(inputString);
    num1 = RandomNumber();
    num2 = RandomNumber();
    
    switch(simple_math)
    {
    case 1:
    JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    break;
    case 2:
    JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    break;
    }
    With this:
    Code:
    inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    simple_math = Integer.parseInt(inputString);
    num1 = RandomNumber();
    num2 = RandomNumber();
    
    switch(simple_math)
    {
    case 1:
    JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    simple_answer = num1 + num2;
    break;
    case 2:
    JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    simple_answer = num1 - num2;
    break;
    }
    simple_answer should then contain the correct answer.


    The second switch statement won't work. You need a boolean condition there...

    if (simple_answer == userinput)
    //correct
    else
    //incorrect
     
    Crunching for Team TPU
  4. YinYang.ERROR

    YinYang.ERROR New Member

    Joined:
    Apr 7, 2009
    Messages:
    463 (0.22/day)
    Thanks Received:
    33
    ^ yeah,

    then he would have to turn this:
    Code:
    switch(simple_answer)
    {
    case 1:
    elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    break;
    case 2:
    elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    break;
    }
    into an if/else which checks if simple_answer is equal to what he input.


    Edit: DAMN YOUR NINJA EDITS!
     
  5. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Hiya! :D
     
    Crunching for Team TPU
  6. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    thanks guys for the quick reply. i will try this out soon. i actually have a couple of weeks to work on it and i think the professor is actually going to finish teaching us how to do it next week. but i figure better to get a jump on it now so i know what kinds of questions to ask in class.
     
  7. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    alright so i replaced the second switch statement with an if/else statement. however i am not sure how to add the condition. i know that if simple_add == userinput but i need to somehow identify userinput as the right answer. here is my new code.

    Code:
    import javax.swing.JOptionPane;
    
    public class elementarymath 
    {
    	private String inputString;
    	public static int response;
    	private int simple_math, simple_answer, num1, num2, add_eq, sub_eq;
    
    	public void userChoice()
    	{
    		inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    		simple_math = Integer.parseInt(inputString);
    		simple_answer = Integer.parseInt(inputString);
    		num1 = RandomNumber();
    		num2 = RandomNumber();
    		
    		switch(simple_math)
    		{
    			case 1:
    				JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    				simple_answer = num1 + num2;
    				break;
    			case 2:
    				JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    				simple_answer = num1 - num2;
    				break;
    		}
    		if (simple_answer == )
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    		else
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    	}
    	public int RandomNumber()
    	{
    		return ((int) (Math.random() * 10)); //generates random number and multiplies by ten
    	}
    	public static void main(String[] args)
    	{
    		elementarymath x = new elementarymath();
    		do
    		{
    			x.userChoice();
    		}
    		while (response == 0);
    		
    		System.exit(0);
    	}
    }
     
  8. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    These two should return the response from the user.
    Code:
    JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    Just as it does here:
    Code:
    inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    You need only compare the stored answer to the answer given by the user (make sure to convert to the same type first).


    You could add a "correct" boolean variable and determine whether or not it is correct inside your switch statement. That would probably be the easiest.

    Example:
    Code:
    int correct = false;
    switch (choice)
    {
      case 1:
        if (Integer.parseInt(OptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = ")) == Integer.parseInt(num1 + num2))
          correct = true;
        break;
    }
    if (correct)
      // correct message
    else
      // incorrect message
    
    That second Integer.parseInt() is there to make certain Java didn't turn it into a double or some other mathematical form which could erroneously return false.
     
    Last edited: Sep 10, 2009
    Easy Rhino says thanks.
    Crunching for Team TPU
  9. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    this is how i am approaching it. but i get the error "type mismatch: cannot convert from string to int"

    here is my new code so you can see exactly what i am trying to do.

    Code:
    import javax.swing.JOptionPane;
    
    public class elementarymath 
    {
    	private String inputString;
    	public static int response;
    	private int simple_math, simple_answer, num1, num2, correct_answer;
    
    	public void userChoice()
    	{
    		inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    		simple_math = Integer.parseInt(inputString);
    		simple_answer = Integer.parseInt(inputString);
    		num1 = RandomNumber();
    		num2 = RandomNumber();
    		correct_answer = inputString;
    		
    		switch(simple_math)
    		{
    			case 1:
    				JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = "); //if user chooses 1 then do this
    				simple_answer = num1 + num2;
    				break;
    			case 2:
    				JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "); //if user chooses 2 then do this
    				simple_answer = num1 - num2;
    				break;
    		}
    		if (simple_answer == correct_answer)
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    		else
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    	}
    	public int RandomNumber()
    	{
    		return ((int) (Math.random() * 10)); //generates random number and multiplies by ten
    	}
    	public static void main(String[] args)
    	{
    		elementarymath x = new elementarymath();
    		do
    		{
    			x.userChoice();
    		}
    		while (response == 0);
    		
    		System.exit(0);
    	}
    }
    
     
  10. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    You still aren't doing anything with the responses from the two "Solve the following addition problem" dialogs.

    Must fix. ;)


    Edit: type mismatch is because string can't be compared to int without first being of the same type. That is, int and int or string and string. In your case, I think you would want to convert the string to an int using Integer.parseInt(). You may want to consider changing it to floats/singles or doubles though otherwise decimals will cause an error.
     
    Crunching for Team TPU
  11. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    sorry im lost now
     
  12. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    I really shouldn't be coding it for you. :p

    Clarify "lost."
     
    Crunching for Team TPU
  13. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    yea i agree. could you point me to some reference material for what i am struggling with?
     
  14. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Google? XD

    To clear up some confusion, I combined all my suggestions with your original code here and commented it. Note that I don't have a Java compiler installed so I didn't try compiling it. It probably isn't perfect but should be close enough to give you an idea:
    Code:
    import javax.swing.JOptionPane;
    
    public class elementarymath 
    {
    	public static int response;
    	
    	public void userChoice()
    	{
    		// Get the choice, convert it to integer, and store it.
    		int choice = Integer.parseInt(JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction"));
    
    		// Randomize values...
    		double a = RandomNumber();
    		double b = RandomNumber();
    
    		// These are variables that are needed in, and referenced outside of, the switch...
    		double user_answer = 0;  // We'll store the user inputted answer here.
    		double comp_answer = 0;  // We'll store the computer calculated answer here
    		Boolean correct = false; // This determines whether or not we consider the two above to match.
    
    		switch(choice)
    		{
    			case 1: // Addition
    				user_answer = Double.parseDouble(JOptionPane.showInputDialog("Solve the following addition problem: \n" + a + " + " + b + " = "));
    				comp_answer = a + b;
    				if (user_answer == comp_answer)
    				{
    					correct = true;
    				}
    				break;
    			case 2: // Subtraction
    				user_answer = Double.parseDouble(JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + a + " - " + b + " = "));
    				comp_answer = a - b;
    				if (user_answer == comp_answer)
    				{
    					correct = true;
    				}
    				break;
    		}
    
    		if (correct)
    		{
    			// It's correct
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    		else
    		{
    			// It's incorrect
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    	}
    
    	// Changed this to double to prevent dataloss.
    	public double RandomNumber()
    	{
    		// Generate a random and round to the closest whole number.
    		return Math.rint(Math.random());
    	}
    
    	// Below this point is unchanged
    	public static void main(String[] args)
    	{
    		elementarymath x = new elementarymath();
    		do
    		{
    			x.userChoice();
    		}
    		while (response == 0);
    
    		System.exit(0);
    	}
    }
    You could display the computational answer if the user answer wasn't correct in that incorrect dialog.
     
    Last edited: Sep 10, 2009
    Easy Rhino says thanks.
    Crunching for Team TPU
  15. YinYang.ERROR

    YinYang.ERROR New Member

    Joined:
    Apr 7, 2009
    Messages:
    463 (0.22/day)
    Thanks Received:
    33

    +1

    Good Job FordGT90Concept! :toast:

    It looks ok to me.
     
  16. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    thanks. i compiled it and ran it and it works i just had to turn some things around. im adjusting my code to add the if/else statements properly. we are still learning that stuff. you have Double.parseDouble what would i do to change that to int as i want all numbers to be single digits.

    edit! nevermind i figured it out. Integer.parseInt

    thanks a ton! now that i know how to put together a decent if/else i will be able to ask some good questions next week and be ahead of the game. i owe you a beer. now what if i want to make it so that with subtraction there cannot be any negative numbers? it would mean that the larger number is always first. so perhaps i need to make another

    int num3 = RandomNumber()

    but have it always be >= 0

    or i could just somehow make num1 > or = 0
     
    Last edited: Sep 10, 2009
  17. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    .NET uses Math.Sign but it appears Java doesn't have that. Quick writeup of equivilent:
    Code:
    // Returns -1 if value is negative, 1 if value is positive, and 0 if value is 0.
    public static int Sign(double value)
    {
      if (value > 0)
      {
        return 1;
      }
      else if (value < 0)
      {
        return -1;
      }
      else
      {
        return 0;
      }
    }

    Double can handle negatives so you don't have to handle it. If you want it to be inside of a certain range, just modify the RandomNumber() method.



    Remember, if you Integer.parseInt() to one side, make sure you do the same to the other. It might not be equal if you don't.


    Example of direct edit to RandomNumber():
    Code:
    	public double RandomNumber()
    	{
    		double rand = -1;  // Forces it to enter the while loop.
    		while (rand < 0)
    		{
    			// Generate a random number until positive.
    			rand = Math.rint(Math.random());
    		}
    		return rand;
    	}
    Edit: I think you would want to put swap code inside of the subtraction case if that's what you want to do. Something like...

    Code:
    			case 2: // Subtraction
    				if (b > a)
    				{
    					double temp = b;
    					b = a;
    					a = temp;
    				}
    				user_answer = Double.parseDouble(JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + a + " - " + b + " = "));
    				comp_answer = a - b;
    				if (user_answer == comp_answer)
    				{
    					correct = true;
    				}
    				break;
     
    Last edited: Sep 10, 2009
    Crunching for Team TPU
  18. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    well i have everything worked out. but i need num1 to always be larger than num2. this is the easiest way to assure that there will never be a negative answer for the subtraction problems. i was thinking something like

    num1 > num2

    but that yields syntax error invalid assignment operator
     
  19. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Look at the bottom of my last post (after the edit). The little if statement there should take care of it no matter where you need it (at least in theory). It is a simple swap before the user even knows it happened. ;)
     
    Crunching for Team TPU
  20. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    damn man. i cant wait to actually learn this stuff in class. :toast:
     
  21. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    You mean you aren't learning it now? :eek: :(
     
    Crunching for Team TPU
  22. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    well yea! sorry i didnt mean it that way!!! i am learning it while you show me the way to do it. that is how i learn. i just meant when we get to the section in class next week that explains it all out in detail.
     
  23. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    i have decided to take a bit of a leap and add a way for the program to keep tabs on how many questions have been answered correctly. it was fairly simple to figure out, however the problem is with the program. when the question has been answered it asks if you want to solve another problem and then if you answer yes it simply restarts the program from the beginning and looses the value! here is what i have so far so far i want it to simply add 1 to the total_correct value and it is doing it with total_correct++ but like i said it looses that value when asking a new question.

    Code:
    import javax.swing.JOptionPane;
    
    public class elementarymath 
    {
    	private String inputString;
    	public static int response;
    	private int simple_math;
    
    	public void userChoice()
    	{
    		inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    		simple_math = Integer.parseInt(inputString);
    		int num1 = RandomNumber();
    		int num2 = RandomNumber();
    		int user_answer = 0;
    		int correct_answer = 0;
    		int total_correct = 0; 
    		boolean correct = false;
    		
    		switch(simple_math)
    		{
    			case 1: //addition
    				user_answer = Integer.parseInt(JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = ")); 
    				correct_answer = num1 + num2;
    				if (user_answer == correct_answer)
    				{
    					correct = true;
    					correct_answer = total_correct++;
    				}
    				break;
    			case 2: //subtraction
    				if (num1 < num2)
    				{
    					int num3 = num1;
    					num2 = num1;
    					num1 = num3;
    				}
    				user_answer = Integer.parseInt(JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "));
    				correct_answer = num1 - num2;
    				if (user_answer == correct_answer)
    				{
    					correct = true;
    				}
    				break;
    		}
    		if(correct)
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! You have answered " + total_correct + " question correctly.\n Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    		else
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    	}
    	public int RandomNumber()
    	{
    		return ((int) (Math.random() * 10)); //generates random number and multiplies by ten
    	}
    	public static void main(String[] args)
    	{
    		elementarymath x = new elementarymath();
    		do
    		{
    			x.userChoice();
    		}
    		while (response == 0);
    		
    		System.exit(0);
    	}
    }
     
  24. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Mainly, you just have to move total_correct outside of the sub to a class-level variable...
    Code:
    import javax.swing.JOptionPane;
    
    public class elementarymath 
    {
    	private String inputString;
    	public static int response;
    	public int total_correct;  // Stuck it here gaining access to it from outside the sub.
    	private int simple_math;
    
    	// Added this constructor to set the value of this instance of total_correct
    	public elementarymath()
    	{
    		total_correct = 0;
    	}
    
    
    	public void userChoice()
    	{
    		inputString = JOptionPane.showInputDialog("Would you like to practice:\n1. Addition\n2. Subtraction");
    		simple_math = Integer.parseInt(inputString);
    		int num1 = RandomNumber();
    		int num2 = RandomNumber();
    		int user_answer = 0;
    		int correct_answer = 0;
    		// Removed it from here and moved to a class-level variable.
    		boolean correct = false;
    		
    		switch(simple_math)
    		{
    			case 1: //addition
    				user_answer = Integer.parseInt(JOptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = ")); 
    				correct_answer = num1 + num2;
    				if (user_answer == correct_answer)
    				{
    					correct = true;
    					correct_answer = total_correct++;
    				}
    				break;
    			case 2: //subtraction
    				if (num1 < num2)
    				{
    					int num3 = num1;
    					num2 = num1;
    					num1 = num3;
    				}
    				user_answer = Integer.parseInt(JOptionPane.showInputDialog("Solve the following subtraction problem: \n" + num1 + " - " + num2 + " = "));
    				correct_answer = num1 - num2;
    				if (user_answer == correct_answer)
    				{
    					correct = true;
    				}
    				break;
    		}
    		if(correct)
    		{
    			total_correct++; // Increment it here.
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "You got it right! You have answered " + total_correct + " question correctly.\n Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    		else
    		{
    			elementarymath.response = JOptionPane.showConfirmDialog(null, "I'm sorry, but that is incorrect. Would you like to continue practicing?\n", "Addition and Subtraction Problems", JOptionPane.YES_NO_OPTION);
    		}
    	}
    	public int RandomNumber()
    	{
    		return ((int) (Math.random() * 10)); //generates random number and multiplies by ten
    	}
    	public static void main(String[] args)
    	{
    		elementarymath x = new elementarymath();  // This line calls the constructor above.
    		do
    		{
    			x.userChoice();
    		}
    		while (response == 0);
    
    		// Show the value of total_correct before closing using x.total_correct
    		
    		System.exit(0);
    	}
    }
    This is only a way to do it; there are many others.
     
    Crunching for Team TPU
  25. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,580 (4.53/day)
    Thanks Received:
    3,415
    damn dude, you are a genious!!!!
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page