我的思路是直接一行读入然后直遍历字符串,利用StringBuilder拼接存储数字
但是这个StringBuilder在压栈的时候会报错显示空
蒟蒻请求大佬帮助
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String s=input.nextLine();
int len=s.length();
Stack<Integer>st=new Stack<>();
for(int i=0;i<len-1;i++) {
StringBuilder sb = new StringBuilder();
char c=s.charAt(i);
if(c>='0'&&c<='9') {
sb.append(c);
}else if(c=='.') {
int temp=Integer.valueOf(sb.toString());
System.out.println(temp);
st.push(temp);
sb.setLength(0);
}else if(c=='+'||c=='-'||c=='/'||c=='*') {
int num2=st.pop();
int num1=st.pop();
if(c=='+') {
st.push(num1+num2);
}else if(c=='-') {
st.push(num1-num2);
}else if(c=='*') {
st.push(num1*num2);
}else if(c=='/') {
st.push(num1/num2);
}
}
}
System.out.println(st.pop());
}
}