1

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

    }
}
Randy Casburn
  • 11,404
  • 1
  • 12
  • 26
Marie
  • 11
  • 1
  • Just declare another variable called prevAnswer and store the calculation in that variable. By default you can give it a 0 value. The code executes from top to bottom, use that to your advantage. If you put a variable later, it will also get updated later. – Goion May 27 '21 at 01:47
  • Why are your doubles static? – code May 27 '21 at 01:54
  • I just tried to assigned it as static but I guess it's not necessary – Marie May 28 '21 at 10:16

3 Answers3

0

You could put the value of the last value outside of the loop. Here's some pseudo-code to demonstrate what I mean:

variableStore = 0
loop:
   perform operations
   when printing out results to the user, assign variableStore = result
Next time, variableStore would be the value of the previous result

code
  • 354
  • 2
  • 12
0

I did some testing and debugging. Try this code(it works for me):

import java.util.Scanner;

public class practice {

static double prevAns = 0;
 
static double another;
 
static double one;

static double add,sub,mul,div;
  
static boolean contin = false;

public static void main (String []args){

  
while(true){
  char choose,cont;
 

Scanner u=new Scanner(System.in);
  
if (!contin){
  System.out.print("Enter number 1: ");

one= Double.parseDouble(u.nextLine());
} else {
  one = prevAns;
  
}
  System.out.print("Enter number 2: ");

double two= Double.parseDouble(u.nextLine());


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;


prevAns = add;

System.out.println("The sum is "+add);}

else if (choose=='B'||choose=='b') {

sub=one-two;

  
prevAns = sub;

System.out.println("The difference is "+sub);

}else if (choose=='C'||choose=='c'){

mul=one*two;

prevAns = mul;

System.out.println("The product is "+mul);

}else if(choose=='D'||choose=='d'){

div=one/two;


prevAns = div;

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'){
contin = true;

} else{

System.out.println("End of Program");

}
}
}  
}

FairOPShotgun
  • 111
  • 13
0

You don't need all those variables. Variable one always stores the latest result. Also, since your class only contains a single method, namely main, there is no need to declare class member variables. Refer to section 6.3 of the Java Language specification that explains about the scope of variables. The link is for Java 7 but is valid for all java versions.

Here is my rewrite of class practice. Note that I changed the class name to Practice, in keeping with Java naming conventions.

import java.util.Scanner;

public class Practice {

    public static void main(String[] args) {
        char choose, cont = 'Y';
        Scanner u = new Scanner(System.in);
        System.out.print("Enter number 1: ");
        double one = Double.parseDouble(u.nextLine());
        double two = 0;
        boolean invalidSelection = false;
        do {
            if (!invalidSelection) {
                System.out.print("Enter another number: ");
                two = Double.parseDouble(u.nextLine());
            }
            invalidSelection = false;
            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.nextLine().charAt(0);
            if ((choose == 'A') || (choose == 'a')) {
                one += two;
                System.out.println("The sum is " + one);
            }
            else if (choose == 'B' || choose == 'b') {
                one -= two;
                System.out.println("The difference is " + one);
            }
            else if (choose == 'C' || choose == 'c') {
                one *= two;
                System.out.println("The product is " + one);
            }
            else if (choose == 'D' || choose == 'd') {
                one /= two;
                System.out.println("The quotient is " + one);
            }
            else {
                System.out.println("Invalid Selection");
                invalidSelection = true;
                continue;
            }
            System.out.print("Continue?[Y/N]: ");
            cont = u.nextLine().charAt(0);
        } while (cont == 'Y' || cont == 'y');
        System.out.println("End of Program");
    }
}

Note, in the above code, that I replaced calls to method next (of class java.util.Scanner) with calls to method nextLine. Refer to this SO question for more details.

Scanner is skipping nextLine() after using next() or nextFoo()?

I recommend that you run the code with a debugger in order to understand how it works.

Here is the output from a sample run.

Enter number 1: 3
Enter another number: 2

Select an operation
[A]Addition
[B]Subtraction
[C]Multiplication
[D]Division

Choose: x
Invalid Selection

Select an operation
[A]Addition
[B]Subtraction
[C]Multiplication
[D]Division

Choose: c
The product is 6.0
Continue?[Y/N]: y
Enter another number: 4

Select an operation
[A]Addition
[B]Subtraction
[C]Multiplication
[D]Division

Choose: b
The difference is 2.0
Continue?[Y/N]: y
Enter another number: 0.5

Select an operation
[A]Addition
[B]Subtraction
[C]Multiplication
[D]Division

Choose: d
The quotient is 4.0
Continue?[Y/N]: n
End of Program
Abra
  • 11,631
  • 5
  • 25
  • 33