Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

java - How to get calculator program to add, subtract, multiply, divide more than two numbers

I have made the basic calculator app which can add, subtract multiply or divide just two numbers. What I am trying to do improve the program to be able to '+' '-' '*' or '/' more than just two numbers. Here is the basic java calculator program I have down so far:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("
Enter first number: 
");
        double fnum = input.nextDouble();
        System.out.println("
Enter an operation sign such as, '+', '-', '*', or '/', '=': 
");
        char operator = input.next().charAt(0);
        System.out.println("
Enter second number: 
");
        double snum = input.nextDouble();
        input.close();

        switch(operator) {

            case '+':
                double answer = fnum + snum;
                System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer);
                break;

            case '-':
                double answer1 = fnum - snum;
                System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer1);
                break;

            case '*':
                double answer2 = fnum * snum;
                System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer2);
                break;

            case '/':
                double answer3 = fnum / snum;
                System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer3);
                break;

            default:
                System.out.println("Wrong choice for operator. ");
                break;
        }
    }
}

To achieve this I was thinking that there has to be a loop probably before the sysout "Enter operator" line and I have tried to incorporate a do while loop with the while part being (operator != '=') and have had no success. Oh yeah and of coarse I need to reword the "Enter second number" sysout. Thanks in advance for any advice!

**Here's an example output of my current calculator program followed by an example of the I output of my desired calculator program.

current calculator output:

8.0 + 2.0 = 10.0

what i'm looking for calculator program to do:

8.0 - 4.0 * 10.0 = 40.0

Note: I am actively working for a solution myself when I have time to do so. If you dont feel like helping me that's perfectly fine. I think my question is clear, valid, and not necessary to delete according to the community guidelines. thanks

question from:https://stackoverflow.com/questions/65886614/how-to-get-calculator-program-to-add-subtract-multiply-divide-more-than-two-n

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The code below does not implement any error checking and, more important, does not take into account the operators precedence - that's why it's better to have a parser - but can give you an idea.

  • the values and the operators are obtained in a loop which is valid until the user enters the = sign

  • the values and the operators entered are stored in the lists numbers and operators

  • after exiting the loop the operations are performed on the stored values


public class Calculator {

    public static void main(String[] args) {
        ArrayList<Double> numbers = new ArrayList<Double>();
        ArrayList<Character> operators = new ArrayList<Character>();
         
        Scanner scanner = new Scanner(System.in);
        try {
            do {
                System.out.println("
Enter a number: 
");
                numbers.add(scanner.nextDouble());
                System.out.println("
Enter an operation sign such as, '+', '-', '*', or '/', '=': 
");
                char operator = scanner.next().charAt(0);
                if (operator == '=')
                    break;
                operators.add(operator);
            } while (true);
        } finally {
            scanner.close();
        }
        
        Double answer = numbers.remove(0);
        String resultText = "" + answer;
        
        for (int i=0; i<operators.size(); ++i) {
            char operator = operators.get(i);
            
            Double number = numbers.get(i);
            
            switch(operator) {

                case '+':
                    answer += number;
                    break;
    
                case '-':
                    answer -= number;
                    break;
    
                case '*':
                    answer *= number;
                    break;
    
                case '/':
                    answer /= number;
                    break;
    
                default:
                    System.out.println("Wrong choice for operator. ");
                    break;
            }
            
            resultText += " " + operator + " " + number;
        }
        
        System.out.println("
" + resultText + " = " + answer);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...