参考 https://blog.csdn.net/duoduo18up/article/details/82193122
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
Stack stack=new Stack();
int result=0;
for(int i=0;i<line.length();i++){
String temp=line.charAt(i)+"";
if(temp.matches("-")){
stack.push('-');
}
else if(temp.matches("\\d")){ //如果是数字
int count=0;
while(!stack.empty()){ //先弹出所有的符号
stack.pop();
count++;
}
for(int j=i+1;j<line.length();j++){ //查看后面是否还有数字
String other=line.charAt(j)+"";
if(other.matches("\\d")) {
temp=temp+other;
i=j;
}
else break;
}
int result_temp=Integer.valueOf(temp);
if(count%2!=0) result_temp=-result_temp;
result=result+result_temp;
}
}
System.out.println(result);
}