有没有办法让终端打印出变量的最后记录值?

is there a way to have the terminal print out the last recorded value of a variable?

我有以下代码。我在终端中输入 'javac Hwk2014.java',然后输入 'java Hwk2014 1 2 + ',这应该给我输出 3。当我使用新生成的数组 'args2' 执行此操作时,我确实得到了 3。当我使用 for 循环输入参数,它清楚地表明 args[0] 是 1 等等。所以它就在那里,但输出一直是'0'

代码:

import java.util.*;

public class Hwk2014 {

    public static void main(String[] args) {
        System.out.println("Hi");
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
        String[] args2 = new String[3];
        args2[0] = "1";
        args2[1] = "2";
        args2[2] = "+";
        /*args2[3] = "3";
        args2[4] = "*";*/
        //System.out.println("+args[0]");
        String value1, value2;
        int addValue = 0, multiplyValue = 0, lastValue = 0;
        //int j = 0, h = 0;
        String addValueS, multiplyValueS;
        Stack<String> object = new Stack<>();
        for(int i = 0; i < args.length; i++)
        {
            object.push(args[i]);
            if (object.size() >= 2 && object.peek() == "+")
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                addValue = Integer.parseInt(value1) + Integer.parseInt(value2);
                addValueS = Integer.toString(addValue);
                object.push(addValueS);
                if (i == (args.length - 1))
                {
                    lastValue = addValue;
                }
            }
            if (object.size() >= 2 && object.peek() == "*")
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                multiplyValue = Integer.parseInt(value1) * Integer.parseInt(value2);
                multiplyValueS = Integer.toString(multiplyValue);
                object.push(multiplyValueS);
                if (i == (args.length - 1))
                {
                    lastValue = multiplyValue;
                }
            }
        }
        System.out.println(lastValue);
    }
}

测试:

Input: ~%java Hwk2014 1 2 + 

output: 0

你的代码对于这么简单的东西来说太复杂了。

public class Main
{
    public static void main(String[] args) throws Exception
    {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);
        String modifier = args[2];
        int result;
        
        switch(modifier) {
            case "+": result=(value1 + value2); break;
            case "-": result=(value1 - value2); break;
            case "*": result=(value1 * value2); break;
            default : result=(value1 / value2); break;
        }
        
        System.out.println("result: " + result);
    }
}
$ java Main 4 2 +
result: 6
$ java Main 4 2 -
result: 2
$ java Main 4 2 \*
result: 8
$ java Main 4 2 \/
result: 2

TL;DR

以下是我对你的问题的解释,但我可能误解了它。您可能只是想将代码写入 Evaluate the Value of an Arithmetic Expression in Reverse Polish Notation in Java

我的回答

下面的代码不检查 [java] command-line 参数是否有效。它假定参数是

的重复
number number operator

其中 operator 是以下之一:

  • +加法
  • /除法
  • *乘法
  • - 减法

请注意,由于运算符字符在命令行中的解释方式,您可能需要将它们分隔开。 (请参阅下面代码后的示例。)

import java.util.Map;
import java.util.Stack;

public class Hwk2014 {
    enum Operator {
        ADD, DIVIDE, MULTIPLY, SUBTRACT;

        public String toString() {
            switch (this) {
                case ADD:
                    return "\uFF0B";
                case DIVIDE:
                    return "\u00F7";
                case MULTIPLY:
                    return "\u00D7";
                case SUBTRACT:
                    return "\u2212";
                default:
                    return this.toString();
            }
        }
    }

    private static int add(int operand1, int operand2) {
        return operand1 + operand2;
    }

    private static void calculate(Operator operator, int operand1, int operand2) {
        double result = Double.MIN_VALUE;
        switch (operator) {
            case ADD:
                result = add(operand1, operand2);
                break;
            case DIVIDE:
                result = divide(operand1, operand2);
                break;
            case MULTIPLY:
                result = multiply(operand1, operand2);
                break;
            case SUBTRACT:
                result = subtract(operand1, operand2);
                break;
            default:
                System.out.println("Unhandled operator: " + operator);
        }
        System.out.printf("%d %s %d = %f%n", operand1, operator, operand2, result);
    }

    private static double divide(int operand1, int operand2) {
        return operand1 / (double) operand2;
    }

    private static double multiply(int operand1, int operand2) {
        return operand1 * operand2 * 1.0d;
    }

    private static int subtract(int operand1, int operand2) {
        return operand1 - operand2;
    }

    public static void main(String[] args) {
        Map<String, Operator> map = Map.of("+", Operator.ADD,
                                           "/", Operator.DIVIDE,
                                           "*", Operator.MULTIPLY,
                                           "-", Operator.SUBTRACT);
        Stack<String> stack = new Stack<>();
        for (String arg : args) {
            stack.push(arg.replace("'", ""));
        }
        Operator operator = null;
        int operand1 = 0;
        int operand2 = 0;
        int remainder = -1;
        for (int i = 0; i < args.length; i++) {
            String val = stack.pop();
            remainder = i % 3;
            switch (remainder) {
                case 0:
                    if (operator != null) {
                        calculate(operator, operand1, operand2);
                    }
                    operator = map.get(val);
                    break;
                case 1:
                    operand1 = Integer.parseInt(val);
                    break;
                case 2:
                    operand2 = Integer.parseInt(val);
                    break;
                default:
                    System.out.println("Problem? remainder = "  + remainder);
            }
        }
        if (remainder == 2  &&  operator != null) {
            calculate(operator, operand1, operand2);
        }
    }
}

这是一个示例命令(在 Windows 10 上):

java Hwk2014 2 1 + 7 6 '*' 7 22 "/" 12 5 "-"

这是输出:

5 − 12 = -7.000000
22 ÷ 7 = 3.142857
6 × 7 = 42.000000
1 + 2 = 3.000000

参考Enum Types+
参考The Map Interface+
参考Characters+
参考Formatting Numeric Print Output+

+部分Oracle's Java tutorials

您的代码

您的代码中有两个问题。这是您更正后的代码,并在代码后对问题进行了解释。我更改了两行,这两行都标有注释 CHANGES HERE

import java.util.*;

public class Hwk2014 {

    public static void main(String[] args) {
        System.out.println("Hi");
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
        String[] args2 = new String[3];
        args2[0] = "1";
        args2[1] = "2";
        args2[2] = "+";
        /*args2[3] = "3";
        args2[4] = "*";*/
        //System.out.println("+args[0]");
        String value1, value2;
        int addValue = 0, multiplyValue = 0, lastValue = 0;
        //int j = 0, h = 0;
        String addValueS, multiplyValueS;
        Stack<String> object = new Stack<>();
        for(int i = 0; i < args.length; i++)
        {
            object.push(args[i]);
            if (object.size() > 2 && "+".equals(object.peek())) // CHANGES HERE
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                addValue = Integer.parseInt(value1) + Integer.parseInt(value2);
                addValueS = Integer.toString(addValue);
                object.push(addValueS);
                if (i == (args.length - 1))
                {
                    lastValue = addValue;
                }
            }
            if (object.size() > 2 && "*".equals(object.peek())) // CHANGES HERE 
            {
                object.pop();
                value1 = object.pop();
                value2 = object.pop();
                multiplyValue = Integer.parseInt(value1) * Integer.parseInt(value2);
                multiplyValueS = Integer.toString(multiplyValue);
                object.push(multiplyValueS);
                if (i == (args.length - 1))
                {
                    lastValue = multiplyValue;
                }
            }
        }
        System.out.println(lastValue);
    }
}

Stack只包含两个元素时,条件object.size() >= 2为真,但您需要它包含三个元素,因此我将其更改为object.size() > 2。那是你的第一个问题。

第二个问题是您没有正确比较字符串。您需要使用方法 equals 而不是运算符 ==。参考How do I compare strings in Java?

当我 运行 你更正的代码时,我得到以下输出:

Hi
Argument 0: 2
Argument 1: 1
Argument 2: +
3