卡在 rpn 计算器问题 java java.lang.String[]

stuck on rpn calculator problem java java.lang.String[]

我一直在尝试在 java 中制作逆波兰符号计算器,但我不断收到错误,我不明白原因。

       private int tokens = ["2","1","+","3","*"]
public class Solution {
    public int evalRPN(String[] tokens) {
        int a,b;
        Stack<Integer> S = new Stack<Integer>();
        for (String s : tokens) {
            if(s.equals("+")) {
                S.add(S.pop()+S.pop());
            }
            else if(s.equals("/")) {
                b = S.pop();
                a = S.pop();
                S.add(a / b);
            }
            else if(s.equals("*")) {
                S.add(S.pop() * S.pop());
            }
            else if(s.equals("-")) {
                b = S.pop();
                a = S.pop();
                S.add(a - b);
            }
            else {
                S.add(Integer.parseInt(s));
            }
        }   
        return S.pop();
    }
}

我刚刚在测试中创建了一个实例 class 并尝试了 运行 intelijij 说 given: string, required string[] or

java: method evalRPN in class calculator cannot be applied to given types;
  required: java.lang.String[]
  found:    no arguments
  reason: actual and formal argument lists differ in length

我知道你来自 python。这里有一些关于您的代码的问题。

1) 您必须在 java.

中以 ; 结束每个变量

2) 看来您想在 java 中创建一个字符串数组。如此处所示

private int tokens = ["2","1","+","3","*"];

问题是当你说一个令牌是一个 int 你必须把它变成一个。所以你真正想要的是一个 String[] 又名字符串数组。所以首先将令牌更改为下面的这个。

private String[] tokens = ["2","1","+","3","*"];

但是等等这仍然会给你一个错误 因为在 java 中数组以 {} 开始和结束所以将它更改为下面的内容并且令牌将工作

private String[] tokens = {"2","1","+","3","*"}; // <-- make sure to add ;

3) public int evalRPN(String[] tokens) 是一种需要将 String[] 数组传入其中的方法。您必须实际调用该方法才能 运行 只是使 private String[] tokens = {"2","1","+","3","*"}; 不会做任何事情。

无论如何下面是一个工作示例你可以复制并粘贴代码并运行这里(https://www.programiz.com/java-programming/online-compiler/). But right after you do that I highly recommend you watch this (https://youtu.be/eIrMbAQSU34)你需要清理很多你对 java 的误解。我无法在 post.

中解释所有这些
import java.util.*; // Import Stack
class HelloWorld {
    
    
    public static void main(String[] args) {
        String[] tokens = {"2","1","+","3","*"}; // <-- make sure to add ;
        
        int value = evalRPN(tokens); // This is what I mean about calling a method
        System.out.println(value);
     
    }
  
    public static int evalRPN(String[] tokens) {
        int a,b;
        Stack<Integer> S = new Stack<Integer>();
        for (String s : tokens) {
            if(s.equals("+")) {
                S.add(S.pop()+S.pop());
            }
            else if(s.equals("/")) {
                b = S.pop();
                a = S.pop();
                S.add(a / b);
            }
            else if(s.equals("*")) {
                S.add(S.pop() * S.pop());
            }
            else if(s.equals("-")) {
                b = S.pop();
                a = S.pop();
                S.add(a - b);
            }
            else {
                S.add(Integer.parseInt(s));
            }
        }   
        return S.pop();
    }

}