if-else疯狂判断
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = bufferedReader.readLine()) != null) {
if(s.equals("END")){
break;
}
Stack<Integer> ints = new Stack<>();
Stack<Character> chars = new Stack<>();
String num = "";
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) < '0' || s.charAt(i) > '9'){
ints.add(Integer.parseInt(num));
num = "";
chars.add(s.charAt(i));
}else{
num += s.charAt(i);
}
}
ints.add(Integer.parseInt(num));
Long cnt = 0L, temp = 1L;
while (!chars.isEmpty()) {
char c = chars.pop();
if(c == '+'){
if(temp == 1L){
cnt += ints.pop();
}else{
cnt += temp * ints.pop();
}
temp = 1L;
}else if(c == '-'){
if(temp == 1L){
cnt -= ints.pop();
}else{
cnt -= temp * ints.pop();
}
temp = 1L;
}else if(c == '*'){
temp *= ints.pop();
}
}
cnt += temp * ints.pop();
System.out.println(cnt);
}
}
}

京公网安备 11010502036488号