You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java使用栈处理逆波兰表达式(RPN)计算结果异常问题求解

逆波兰表达式Java实现修复方案

核心问题原因

  • 运算符判断逻辑错误:使用==比较字符串内容,Java中==仅对比字符串对象的内存地址,不会判断内容是否相等,导致所有运算符都被误判定为数值压入栈中,最终栈顶元素是运算符,转换为整数时抛出异常,触发非法表达式报错。
  • 输入处理代码存在语法错误:不能直接将字符串赋值给字符串数组变量。

具体修复操作

  1. 将运算符判断的==替换为字符串equals()方法,推荐用常量字符串调用equals()避免空指针异常
  2. 修正输入处理的语法错误
  3. (可选优化)可将栈的泛型改为Integer,避免运算过程中反复做字符串和整数的类型转换,降低异常概率

修正后代码

输入处理部分

String line = "8 6 + 2 /"; // 输入的表达式字符串
String[] collect = line.split(" "); // 转换为字符串数组
System.out.println(stackem(collect)); // 调用栈处理方法输出结果

栈处理方法部分

public String stackem(String[] input) {
    Stack<String> stack = new Stack<String>();
    int x, y;
    String result = "";
    String choice;
    int value = 0;
    String p = "";
    int output;

    try {
        for (int i = 0; i < input.length; i++) {
            // 修正字符串比较逻辑
            if ("+".equals(input[i]) || "-".equals(input[i]) || "*".equals(input[i]) || "/".equals(input[i]) || "^".equals(input[i])) {
                choice = input[i];
            } else {
                stack.push(input[i]);
                continue;
            }

            switch (choice) {
                case "+":
                    x = Integer.parseInt(stack.pop());
                    y = Integer.parseInt(stack.pop());
                    value = x + y;
                    result = p + value;
                    stack.push(result);
                    break;
                case "-":
                    x = Integer.parseInt(stack.pop());
                    y = Integer.parseInt(stack.pop());
                    value = y - x;
                    result = p + value;
                    stack.push(result);
                    break;
                case "*":
                    x = Integer.parseInt(stack.pop());
                    y = Integer.parseInt(stack.pop());
                    value = x * y;
                    result = p + value;
                    stack.push(result);
                    break;
                case "/":
                    x = Integer.parseInt(stack.pop());
                    y = Integer.parseInt(stack.pop());
                    value = y / x;
                    result = p + value;
                    stack.push(result);
                    break;
                case "^":
                    x = Integer.parseInt(stack.pop());
                    y = Integer.parseInt(stack.pop());
                    value = (int)Math.pow(y,x);
                    result = p + value;
                    stack.push(result);
                    break;
                default:
                    continue;
            }
        }
        output = Integer.parseInt(stack.pop());
    } catch (Exception ex) {
        return "Error: Expression is malformed";
    }
    return "Result: " + output;
}

验证结果

输入8 6 + 2 /时,程序执行流程为:8入栈→6入栈→遇到+弹出6、8求和得14入栈→2入栈→遇到/弹出2、14求商得7入栈→最终弹出7返回,符合预期输出。

内容的提问来源于stack exchange,提问作者Michael Calle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 19:24:02