- Joined
- Nov 13, 2006
- Messages
- 15,643 (2.35/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 | Fedora KDE Spin |
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);
}
}
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);
}
}