編譯沒有問題,但是不管是否有while循環,結果都是一樣的。我無法理解爲什麼包含while循環。順便說一句,這僅僅是從Java SE教程的示例程序:在下面的程序中,while循環的目的是什麼?
public class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";
String substring = "sub";
boolean foundIt = false;
int max = searchMe.length() - substring.length();
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) { // WTF???
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}
'n'正在從'substring.length()'向下計數到'0' – MadProgrammer
@MadProgrammer通常是的,但'繼續測試;''裏面'while'使它不那麼容易... – Pshemo
@MadProgrammer我不知道認爲是這樣,b/c然後它只會運行這個循環3次,但它一直持續下去,直到找到(或不是)子字符串。另外,如果我把while循環(包括適當的大括號),程序的結果是相同的。必須有一些有效的原因,他們把它英寸 – VisWebsoft