I'm making a calculator where you ask two numbers and an operation. If the user wants to continue, ask for a number and an operation. Then, perform the selected operation with the recent result and the new input value. I'm stuck in storing the result of the last operation to use it for another operation. Here's my code
import java.util.Scanner;
public class practice {
static double add, sub, mul, div;
static double another;
public static void main(String[] args) {
char choose, cont;
Scanner u = new Scanner(System.in);
System.out.print("Enter number 1: ");
double one = Double.parseDouble(u.nextLine());
System.out.print("Enter number 2: ");
double two = Double.parseDouble(u.nextLine());
do {
System.out.print("\nSelect an operation\n[A]Addition\n[B]Subtraction\n[C]Multiplication\n[D]Division");
System.out.print("\n\nChoose: ");
choose = u.next().charAt(0);
if ((choose == 'A') || (choose == 'a')) {
add = one + two;
add += another;
System.out.println("The sum is " + add);
} else if (choose == 'B' || choose == 'b') {
sub = one - two;
sub -= another;
System.out.println("The difference is " + sub);
} else if (choose == 'C' || choose == 'c') {
mul = one * two;
mul *= another;
System.out.println("The product is " + mul);
} else if (choose == 'D' || choose == 'd') {
div = one / two;
div /= another;
System.out.println("The quotient is " + div);
} else {
System.out.println("Invalid Selection");
}
System.out.print("Continue?[Y/N]: ");
cont = u.next().charAt(0);
if (cont == 'Y') {
System.out.print("Enter another number: ");
another = u.nextDouble();
another = another;
} else {
System.out.println("End of Program");
}
} while (cont == 'Y');
}
}