有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

创建Java堆栈

public static int evaluate(Scanner input)
    {
        if (input.hasNextInt())
        {
            return input.nextInt();
        }
        else
        {
            String operator = input.next();
            int operand1 = evaluate(input);
            int operand2 = evaluate(input);
            return evaluate(operator, operand1, operand2);
        }
    }

    // pre : operator is one of *, /, %, + or -
    // post: returns the result of applying the given operator to
    //       the given operands
    public static int evaluate(String operator, int operand1, int operand2)
    {
        if (operator.equals("*"))
        {
            return operand1 * operand2;
        }
        else if (operator.equals("/"))
        {
            return operand1 / operand2;
        }
        else if (operator.equals("%"))
        {
            return operand1 % operand2;
        }
        else if (operator.equals("+"))
        {
            return operand1 + operand2;
        }
        else if (operator.equals("-"))
        {
            return operand1 - operand2;
        }
        else
        {
            throw new RuntimeException("illegal operator " + operator);
        }
    }

我想把这段代码转换成两个堆栈(一个堆栈用于运算符,另一个堆栈用于操作数),以便在带有actionlistener的用户输入GUI中使用前缀表达式。如何编写代码,将此代码转换为两个堆栈?顺便说一句,这是家庭作业,我知道你不能直接给我答案,所以如果你能给我提供易于理解的伪代码,我将不胜感激。谢谢你的帮助


共 (0) 个答案