问题遇到的现象和发生背景
第23行将i<s.length()放到 (c=s.charAt(i))>= 48 && (c=s.charAt(i)) <= 57后面就会报错,放到前面就能正常运行,没有想明白为什么会这样
问题相关代码,请勿粘贴截图
package 栈;
import java.util.ArrayList;
import java.util.List;
public class PolandNotation {
public static void main(String[] args) {
List<String> infixExpression = toInfixExpression("3+4*50-6");
System.out.println(infixExpression);
}
public static List<String> toInfixExpression(String s) {
List<String> ls = new ArrayList<>();
String str;
int i = 0;
char c;
do {
if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
ls.add("" + c);
i++;
} else {
str ="";
while ( i<s.length() && (c=s.charAt(i))>= 48 && (c=s.charAt(i)) <= 57 ){
str += c;
i++;
}
ls.add(str);
}
} while (i < s.length());
return ls;
}
}
class operation{
private static int ADD=1;
private static int SUB=1;
private static int MUL=2;
private static int DIV=2;
public static int getVaule(String operation){
int result=0;
switch(operation){
case "+":
result=ADD;
break;
case "-":
result=SUB;
break;
case "*":
result=MUL;
break;
case "/":
result=DIV;
break;
default:
System.out.println("不存在该运算符" + operation);
break;
}
return result;
}
}