Java计算器程序问题

发布于 2025-01-16 10:19:46 字数 3415 浏览 0 评论 0原文

我正在尝试开发一个计算器程序,该程序输入 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!");
        
    }
}

I am trying to develop a calculator program that inputs an arithmetic expression of the form number operator number = and computes the result of the expression. The expression will be evaluated from left to right not considering regular operator precedence. For example, the expression 14 - 5 * 3 = will produce 27.0. The value = displays the final result and terminates the program.

I've been trying to fix this for a couple of days now, but it outputs the wrong answer whenever I enter an expression with more than two numbers. For instance, 2.8 + 2 - 9.5 should equal -4.7 but the program outputs -6.7. Any idea why that is the case?

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!");
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2025-01-23 10:19:46

为了使其工作,请尝试:

  • 在第二个 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!");
        
    }
}

In order to make it work, try to:

  • in your 2nd switch statement, change num2 = num1 + num2; into num1 = num1 + num2;. Do this for all cases;
  • I added an isOperator boolean to skip computing the operation if input is an operator.

Full code below:

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!");
        
    }
}

Edit: As mentioned in the comments, the code does not treat the cases when the user inputs 0. Below, I removed the if(num1 == 0) and if (num1 != 0 && num2 != 0) conditions:

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!");
        
    }
}
莫言歌 2025-01-23 10:19:46

我稍微改变了你的顺序并重置了保持变量。

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

I switched up your order a little bit and reset the holding variable.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文