Given the code fragment:



String[] colors = {"red", "blue", "green", "yellow", "maroon", "cyan"};

Which code fragment prints blue, cyan, ?

A.

for (String c : colors) {
    if (c.length() != 4) {
        continue;
    }
    System.out.print(c + ", ");
}
B.
for (String c : colors[]) {
    if (c.length() <= 4) {
        continue;
    }
    System.out.print(c + ", ");
}
C.
for (String c : String[] colors) {
    if (c.length() >= 4) {
        continue;
    }
    System.out.print(c + ", ");
}
D.
for (String c : colors) {
    if (c.length() >= 4) {
        System.out.print(c + ", ");
        continue;
    }
}

題解

選項B、C是錯誤的foreach用法,會編譯錯誤。

選項A,會把所有字串長度等於4的元素輸出,是正確答案。

選項D,會把所有字串長度大於等於4的元素輸出,不符合題目要求。