Given:
public class Whizlabs {
public static void main(String[] args) {
String s = "A";
switch (s) {
case "a":
System.out.println("simaple A ");
default:
System.out.print("default ");
case "A":
System.out.print("Capital A ");
}
}
}
What is the result?
A.
simaple A
B.
Capital A
C.
simaple A default Capital A
D.
simaple A default
E. Compilation fails.
題解
switch要選擇的內容是s變數所參考到的「A」字串,因此會執行第11行的case,輸出「Capital A 」後就離開switch了。