请问为什么这段代码while循环内一旦进入了else后面部分,之后的while循环就无法再进入前半个分支了?我看if 后面的条件应该是满足的啊,运行结果由于进入不了第一个循环就没法break了。求大神指教:
public class SearchString {
public static void main(String[] args) {
String s1 = "abcaaaaaaaabcabc";
String s2 = "abc";
int len = s2.length();
int pos = 0;
int count = 0;
while (true) {
if (s1.indexOf(s2, pos) == -1) {
System.out.println("Search Over, result count=" + count);
break;
} else {
System.out.println("Position" + (count + 1) + " is " + pos);
int a = pos + len;
pos = s1.indexOf(s2, a);
count++;
}
}
}
}
运行结果就是-1 10 13 -1 10 13无限循环了
扬帆大鱼
胡说叔叔
相关分类