简体   繁体   English

如何让计算器程序对两个以上的数字进行加减乘除

[英]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:这是到目前为止我已经完成的基本 java 计算器程序:

import java.util.Scanner;

public class Calculator {

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

        switch(operator) {

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

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

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

            case '/':
                double answer3 = fnum / snum;
                System.out.println("\n" + 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.为了实现这一点,我认为可能在 sysout“Enter operator”行之前必须有一个循环,我试图将一个 do while 循环与 while 部分合并为 (operator.= '=') 并且没有成功. 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. **这是我当前计算器程序的示例 output,然后是我想要的计算器程序的 I output 示例。

current calculator output:电流计算器 output:

8.0 + 2.0 = 10.0 8.0 + 2.0 = 10.0

what i'm looking for calculator program to do:我正在寻找计算器程序来做什么:

8.0 - 4.0 * 10.0 = 40.0 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谢谢

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输入的值和运算符存储在列表numbersoperators

  • 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("\nEnter a number: \n");
                numbers.add(scanner.nextDouble());
                System.out.println("\nEnter an operation sign such as, '+', '-', '*', or '/', '=': \n");
                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("\n" + resultText + " = " + answer);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM