Hello all. I am new to Java and I am have some issues with some code I'm working on. I am having a problem with looping a while loop that contains a switch statement. Here is my code:
package hw4jenningsd;
import javax.swing.*;
public class HW4JenningsD {
public static class mathOps {
private int numOne;
private int numTwo;
private int numThreeOne;
private int numThreeTwo;
private String inputStringOne;
private String inputStringTwo;
public mathOps() {
numOne = 0;
numTwo = 0;
numThreeOne = 0;
numThreeTwo = 0;
} //end constructor
public void input() {
inputStringOne = JOptionPane.showInputDialog(null,"Enter your first "
+ "integer number");
numOne = Integer.parseInt(inputStringOne);
inputStringTwo = JOptionPane.showInputDialog(null,"Enter you second "
+ "integer number");
numTwo = Integer.parseInt(inputStringTwo);
}// end input
public void addition() {
numThreeOne = numOne + numTwo;
numThreeTwo = numOne + numTwo;
}// end addtion
public void subtraction() {
numThreeOne = numOne - numTwo;
numThreeTwo = numTwo - numOne;
}// end subtraction
public void multiplication() {
numThreeOne = numOne * numTwo;
numThreeTwo = numTwo * numOne;
}// end multiplication
public void division() {
numThreeOne = numOne / numTwo;
numThreeTwo = numTwo / numOne;
}// end division
public void modulo() {
numThreeOne = numOne % numTwo;
numThreeTwo = numTwo % numOne;
}// end modulo
public void displayResult() {
JOptionPane.showMessageDialog(null, "The result of your "
+ "calculations are " + numThreeOne + " and " +numThreeTwo+"\n and the two integers "
+ "you entered were "+numOne+ " and " +numTwo+ "\n and the type "
+ "of operation performed was "); //another problem. How can I code into this line the type of math operation was performed?
}// end displayResult
}// end class mathOps
public static void main(String[] args) {
String operationString;
int repeatOperation2;
int operationChoice;
mathOps mathOpsOne = new mathOps();
mathOpsOne.input();
JOptionPane.showMessageDialog(null,"Math operations Menu\nThe "
+ "purpose of this menu is to allow the user "
+ "to choose a\nmath operation to be performed.\n\n"
+ "1. +\n2. -\n3. *\n4. /\n5. %\n6. End the program.");
repeatOperation2 = 1;
operationString = JOptionPane.showInputDialog(null,"Press 1 to add,"
+ " 2 to subtract, 3 to multiply, 4 to divide, 5 for modulo, "
+ "or 6 to end the program");
operationChoice = Integer.parseInt(operationString);
while(repeatOperation2 == 1) {
switch(operationChoice) {
case 1: mathOpsOne.addition();
break;
case 2: mathOpsOne.subtraction();
break;
case 3: mathOpsOne.multiplication();
break;
case 4: mathOpsOne.division();
break;
case 5: mathOpsOne.modulo();
break;
case 6: JOptionPane.showMessageDialog(null,"Thanks for using "
+ "my program.");System.exit(0);
break;
default:JOptionPane.showInputDialog(null,"The selection is not"
+ " acceptable.\n Enter 0 to try again.");
break;
}// end switch operationChoice
mathOpsOne.displayResult();repeatOperation2 = 2;
while(repeatOperation2 == 2){
repeatOperation2 = Integer.parseInt(JOptionPane
.showInputDialog("Do you want to use the same "
+ "integers or a new set of integers" +
"\nPress [0] for new integers or [1] for same integers."));// one more problem. If the user chooses the same integers, how can I code it so that the user does not enter a new set of integers?
if(repeatOperation2 == 0) //this is where my problem is. I want the program to loop back to the math menu.
mathOpsOne.input();
else if(repeatOperation2 == 1){
repeatOperation2 = 1;
}
}
}// end while
JOptionPane.showMessageDialog(null,"Thanks for using "
+ "my program. ");System.exit(0);
}// end main
}// end program
Thanks for any assistance.