Java计算器程序问题

Java Calculator program issue

我正在尝试开发一个计算器程序,它输入 number operator number = 形式的算术表达式并计算表达式的结果。表达式将从左到右计算,不考虑常规运算符的优先级。例如,表达式 14 - 5 * 3 = 将产生 27.0。值=显示最终结果并终止程序。

几天来我一直在尝试解决这个问题,但每当我输入包含两个以上数字的表达式时,它都会输出错误的答案。例如,2.8 + 2 - 9.5 应该等于 -4.7 但程序输出 -6.7。知道为什么会这样吗?

import java.util.Scanner;

public class Calculator {

    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double num1 = 0;
        double num2 = 0;
        char operator = 0;
        
        Scanner scnr = new Scanner (System.in);
        
        System.out.println("Enter your numeric expression in the following form: ");
        System.out.println("number operator number operator number = ");
        System.out.println("Leave a blank space after each number or operator.");
        System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

        // Input the first item
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            switch (input){
                case "+":               
                    operator = '+';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "-":               
                    operator = '-';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "*": 
                    operator = '*';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "/": 
                    operator = '/';
                    System.out.println("> Operator is: " + operator);
                    break;                  
                default: // a number was entered
                    if (num1 == 0) {
                        num1 = Double.parseDouble(input);
                        System.out.println("> Num1 is: " + num1);
                    }
                    else {
                        num2 = Double.parseDouble(input);
                        System.out.println("> Num2 is: " + num2);
                    }
                    
            } // end of switch
            
            if (num1 != 0 && num2 != 0) {
                
                System.out.println("Num2 before calc is " + num2);
                
                switch (operator) {
                    case '+':               
                        num2 = num1 + num2;
                        break;
                    case '-':               
                        num2 = num1 - num2;
                        break;
                    case '*': 
                        num2 = num1 * num2;
                        break;
                    case '/': 
                        num2 = num1 / num2;
                        break;
                    default:
                }
            }
            input = scnr.next();
            
        } // end of while-loop
        
        // Display the answer
        System.out.println("> Answer is: " + num2);
        
        
        
        System.out.println("Have a nice day!");
        
    }
}

我稍微调整了你的订单并重置了持有变量。

public static void main(String[] args) {
     // Declare the identifiers
    final String END = "=";
    String input;
    double num1 = 0;
    double num2 = 0;
    char operator = 0;
    
    Scanner scnr = new Scanner (System.in);
    
    System.out.println("Enter your numeric expression in the following form: ");
    System.out.println("number operator number operator number = ");
    System.out.println("Leave a blank space after each number or operator.");
    System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

    // Input the first item
    System.out.print("> ");
    input = scnr.next();
    
    // Process the first item and input and process the rest of the items 
    while (!input.equals(END)){
        switch (input){
            case "+":               
                operator = '+';
                System.out.println("> Operator is: " + operator);
                break;
            case "-":               
                operator = '-';
                System.out.println("> Operator is: " + operator);
                break;
            case "*": 
                operator = '*';
                System.out.println("> Operator is: " + operator);
                break;
            case "/": 
                operator = '/';
                System.out.println("> Operator is: " + operator);
                break;                  
            default: // a number was entered
                if (num1 == 0) {
                    num1 = Double.parseDouble(input);
                    System.out.println("> Num1 is: " + num1);
                } else {
                    num2 = Double.parseDouble(input);
                    System.out.println("> Num2 is: " + num2);
                }
                
        } // end of switch
        
        if (num1 != 0 && num2 != 0) {
            
            System.out.println(String.format("Num1 : %.3f, Num2: %.3f", num1, num2));
            
            switch (operator) {
                case '+':               
                    num1 = num1 + num2;
                    num2 = 0;
                    break;
                case '-':               
                    num1 = num1 - num2;
                    num2 = 0;
                    break;
                case '*': 
                    num1 = num1 * num2;
                    num2 = 0;
                    break;
                case '/': 
                    num1 = num1 / num2;
                    num2 = 0;
                    break;
                default:
            }
        }
        input = scnr.next();
        
    } // end of while-loop
    
    // Display the answer
    System.out.println("> Answer is: " + num1);
}

为了让它发挥作用,请尝试:

  • 在您的第二个 switch 语句中,将 num2 = num1 + num2; 更改为 num1 = num1 + num2;。对所有情况都这样做;
  • 如果输入是运算符,我添加了一个 isOperator 布尔值以跳过计算操作。

完整代码如下:

import java.util.Scanner;

public class Calculator {

    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double num1 = 0;
        double num2 = 0;
        char operator = 0;
        boolean isOperator;
        
        Scanner scnr = new Scanner (System.in);
        
        System.out.println("Enter your numeric expression in the following form: ");
        System.out.println("number operator number operator number = ");
        System.out.println("Leave a blank space after each number or operator.");
        System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

        // Input the first item
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            isOperator = true;
            switch (input){
                case "+":               
                    operator = '+';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "-":               
                    operator = '-';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "*": 
                    operator = '*';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "/": 
                    operator = '/';
                    System.out.println("> Operator is: " + operator);
                    break;                  
                default: // a number was entered
                    isOperator = false;
                    if (num1 == 0) {
                        num1 = Double.parseDouble(input);
                        System.out.println("> Num1 is: " + num1);
                    }
                    else {
                        num2 = Double.parseDouble(input);
                        System.out.println("> Num2 is: " + num2);
                    }
                    
            } // end of switch
            
            // do not compute the operation if the input is an operator and num1,num2 != 0
            if (num1 != 0 && num2 != 0 && !isOperator) {
                
                System.out.println("Num2 before calc is " + num2);
                
                switch (operator) {
                    case '+':               
                        num1 = num1 + num2;
                        break;
                    case '-':               
                        num1 = num1 - num2;
                        break;
                    case '*': 
                        num1 = num1 * num2;
                        break;
                    case '/': 
                        num1 = num1 / num2;
                        break;
                    default:
                }
            }
            input = scnr.next();
            
        } // end of while-loop
        
        // Display the answer
        System.out.println("> Answer is: " + num1);
        
        
        
        System.out.println("Have a nice day!");
        
    }
}

编辑: 如评论中所述,代码不处理用户输入 0 的情况。下面,我删除了 if(num1 == 0)if (num1 != 0 && num2 != 0) 条件:

import java.util.Scanner;

public class Calculator {

    // Compute an arithmetic expression 
    public static void main(String[] args) {
        // Declare the identifiers
        final String END = "=";
        String input;
        double result = 0;
        double num = 0;
        char operator = 0;
        boolean isOperator;
        
        Scanner scnr = new Scanner (System.in);
        
        System.out.println("Enter your numeric expression in the following form: ");
        System.out.println("number operator number operator number = ");
        System.out.println("Leave a blank space after each number or operator.");
        System.out.println("Example: 3.5 * 3 - 5 / 2.5 =");

        // Input the first item
        System.out.print("> ");
        input = scnr.next();
        
        // Process the first item and input and process the rest of the items 
        while (!input.equals(END)){
            isOperator = true;
            switch (input){
                case "+":               
                    operator = '+';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "-":               
                    operator = '-';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "*": 
                    operator = '*';
                    System.out.println("> Operator is: " + operator);
                    break;
                case "/": 
                    operator = '/';
                    System.out.println("> Operator is: " + operator);
                    break;                  
                default: // a number was entered
                    isOperator = false;
                    num = Double.parseDouble(input);
                    System.out.println("> Num is: " + num);
            } // end of switch
            
            // do not compute the operation if the input is an operator
            if (!isOperator) {
                
                System.out.println("Result before calc is " + result);
                
                switch (operator) {
                    case '+':               
                        result += num;
                        break;
                    case '-':               
                        result -= num;
                        break;
                    case '*': 
                        result *= num;
                        break;
                    case '/': 
                        result /= num;
                        break;
                    default:
                        result += num;
                }
            }
            input = scnr.next();
            
        } // end of while-loop
        
        // Display the answer
        System.out.println("> Answer is: " + result);
        
        
        
        System.out.println("Have a nice day!");
        
    }
}