0

I'm trying to get only one input from the user and do the calculation with the previous result. The calculator starts at 0, although I know there could be some problems for multiplication. For now that is not important. I've been stuck with this for a while and I can't seem to find a solution for it. So this would look something like this:

0
+ 3 (user input)
3 (result)
- 2 (user input)
1 (result)

The code does that a couple of times but I need a loop somehow so that it does that until the user says something like exit or e or something else.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Please enter your calculation");
        Scanner scanner = new Scanner(System.in);
        System.out.println("0");
        //int left = scanner.nextInt();
        String op = scanner.next();
        int right = scanner.nextInt();
        int num3 = compute(0,op,right);
        System.out.println(num3);
        int rright;
        int rrright;
        System.out.println("Enter your calculation");
        String op1 = scanner.next();
        rright = scanner.nextInt();
        int num4 = compute(num3,op1,rright);
        System.out.println(num4);
        System.out.println("Enter your calculation");
        String op2 = scanner.next();
        rrright = scanner.nextInt();
        int num5 = compute(num4,op2,rrright);
        System.out.println(num5);
    }

    private static int compute(int left, String op, int right) {
        switch (op.charAt(0)) {
            case '+':
                return left + right;
            case '-':
                return left - right;
            case '*':
                return left * right;
            case '/':
                return left / right;
        }
        throw new IllegalArgumentException("Unknown operator:" + op);
    }
}
TiiJ7
  • 3,152
  • 4
  • 25
  • 35
Ichmag17
  • 1
  • 3
  • 3
    1) Use a `while(true)` loop and then `break` out of it if the user inputs a specific keyword. 2) Have a look at [this question](https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0), division of integers in java will not work the way you expect – Lino Feb 27 '19 at 12:53

1 Answers1

0

A while loop should do the job. Also you should use double instead of int, so that the result of a division is calculated correctly (as Lino suggested in his comment). The code would look like this:

public static void main(String[] args) {
    System.out.println("Please enter your calculation");
    Scanner scanner = new Scanner(System.in);
    System.out.println("0");
    double result = 0, right;
    String op;
    while(true){
        System.out.println("Enter your calculation");
        op = scanner.next();
        if(op.equals("exit")) break;
        right = scanner.nextDouble();
        result = compute(result, op, right);
        System.out.println(result);
    }
    System.out.println("end of program");
}

private static double compute(double left, String op, double right) {
    switch (op.charAt(0)) {
        case '+':
            return left + right;
        case '-':
            return left - right;
        case '*':
            return left * right;
        case '/':
            return left / right;
    }
    throw new IllegalArgumentException("Unknown operator:" + op);
}

Note: scanner.nextDouble() expects the number comma-separated (e.g. 4,8).

Max.-F.
  • 145
  • 1
  • 8