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

Rhino's "I need Java help" thread

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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);
}
}
 

YinYang.ERROR

New Member
Joined
Apr 7, 2009
Messages
463 (0.08/day)
System Name Echelon
Processor Phenom II x3 720 - 4th Core Unlocked
Motherboard Asus M4A785TD-V EVO
Cooling Stock
Memory Adata 4gb ddr3 1600
Video Card(s) Ati HD 5770
Storage 640gb & 250gb SATA
Display(s) MAG CRT 17"
Case CM 690
Audio Device(s) onboard
Power Supply 600w Cooler Master Silent Pro
Software Windows 7 Ultimate x64
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?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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
 

YinYang.ERROR

New Member
Joined
Apr 7, 2009
Messages
463 (0.08/day)
System Name Echelon
Processor Phenom II x3 720 - 4th Core Unlocked
Motherboard Asus M4A785TD-V EVO
Cooling Stock
Memory Adata 4gb ddr3 1600
Video Card(s) Ati HD 5770
Storage 640gb & 250gb SATA
Display(s) MAG CRT 17"
Case CM 690
Audio Device(s) onboard
Power Supply 600w Cooler Master Silent Pro
Software Windows 7 Ultimate x64
^ 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!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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);
	}
}
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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:
case 1:
  if (Integer.parseInt(OptionPane.showInputDialog("Solve the following addition problem: \n" + num1 + " + " + num2 + " = ")) == (num1 + num2))
    correct = true;
  else
    correct = false;
  break;

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

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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.

sorry im lost now
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I really shouldn't be coding it for you. :p

Clarify "lost."
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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:

YinYang.ERROR

New Member
Joined
Apr 7, 2009
Messages
463 (0.08/day)
System Name Echelon
Processor Phenom II x3 720 - 4th Core Unlocked
Motherboard Asus M4A785TD-V EVO
Cooling Stock
Memory Adata 4gb ddr3 1600
Video Card(s) Ati HD 5770
Storage 640gb & 250gb SATA
Display(s) MAG CRT 17"
Case CM 690
Audio Device(s) onboard
Power Supply 600w Cooler Master Silent Pro
Software Windows 7 Ultimate x64
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:


+1

Good Job FordGT90Concept! :toast:

It looks ok to me.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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.

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:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
.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:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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. ;)
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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. ;)

damn man. i cant wait to actually learn this stuff in class. :toast:
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You mean you aren't learning it now? :eek: :(
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
You mean you aren't learning it now? :eek: :(

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.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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);
	}
}
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,445 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
damn dude, you are a genious!!!!
 
Top