Java中Switch-Case用法小結(jié)
1、基礎(chǔ)知識
java中switch-case語句的一般格式如下:
switch(參數(shù))?{?? ????case?常量表達式1:?break;?? ????case?常量表達式2:?break;?? ????...?? ????default:?break;?? }??
注解:
(1)、switch接受的參數(shù)類型有10種,分別是基本類型的byte,short,int,char,以及引用類型的String(只有JavaSE 7 和以后的版本 可以接受String類型參數(shù)),enum和byte,short,int,char的封裝類Byte,Short,Integer,Character
case 后緊跟常量表達式,不能是變量。
default語句可有可無,如果沒有case語句匹配,default語句會被執(zhí)行。
case語句和default語句后的代碼可不加花括號。
如果某個case語句匹配,那么case后面的語句塊會被執(zhí)行,并且如果后面沒有break關(guān)鍵字,會繼續(xù)執(zhí)行后面的case語句代碼和default,直到遇見break或者右花括號。
2、4個測試用例
(1)、測試一
代碼:
package cn.test;
public class SwitchCase01 {
public static void main(String[] args) {
int i = 5;
switch (i) {
case 1:
System.out.println("one");
break;
case 10:
System.out.println("ten");
break;
case 5:
System.out.println("five");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
break;
}
/**
* 返回結(jié)果: five
*
*
*/
}
}
結(jié)果:
分析:
????在本案例測試中,switch-case語句遵循其基本定義、語法,case語句、default語句沒有出現(xiàn)錯位情況,且case語句、default語句代碼塊中都包含break關(guān)鍵字,用于結(jié)束switch-case語句。
(2)、測試二
代碼:
package cn.test;
public class SwitchCase02 {
public static void main(String[] args) {
int i = 5;
switch (i) {
case 1:
System.out.println("one");
case 10:
System.out.println("ten");
case 5:
System.out.println("five");
case 3:
System.out.println("three");
default:
System.out.println("other");;
}
}
}
結(jié)果:
分析:?
在本案例測試中,case語句、default語句的代碼塊中不包含break關(guān)鍵字。當switch(i)中的變量在case語句中匹配后,case語句的代碼塊被執(zhí)行,由于該case語句的代碼塊中沒有break關(guān)鍵字用于結(jié)束switch。故在switch-case中,程序繼續(xù)執(zhí)行后面的case語句代碼和default,直到遇見break或者右花括號。
(3)、測試一
代碼:
package cn.test;
public class SwitchCase03 {
public static void main(String[] args) {
String x = "three";
switch (x) {
case "one":
System.out.println("switch表達式中類型可用String類型,輸入字符串為:" + x);
break;
case "two":
System.out.println("switch表達式中類型可用String類型,輸入字符串為:" + x);
break;
case "three":
System.out.println("switch表達式中類型可用String類型,輸入字符串為:" + x);
break;
case "four":
System.out.println("switch表達式中類型可用String類型,輸入字符串為:" + x);
break;
default:
System.out.println("switch表達式中類型可用String類型");
break;
}
}
}
分析:
????在本案例測試中,主要是驗證switch()-case語句中,switch()函數(shù)中的變量是否支持String類型。經(jīng)驗證,switch-case語句中,switch()函數(shù)中支持String類型。需要注意的是:在JDK1.7及以上版本中,Switch()函數(shù)中變量才支持String類型。
(4)、測試一
代碼:
package cn.test;
public class SwitchCase04 {
public static void main(String[] args) {
String x = "three";
//String x = "six";
System.out.println(TestSwitch(x));
}
public static String TestSwitch(String x) {
switch (x) {
case "one":
x = "1";
return "return關(guān)鍵字對case代碼塊中的影響,輸出值為:" + x ;
case "two":
x = "2";
return "return關(guān)鍵字對case代碼塊中的影響,輸出值為:" + x ;
case "three":
x = "3";
return "return關(guān)鍵字對case代碼塊中的影響,輸出值為:" + x ;
case "four":
x = "4";
return "return關(guān)鍵字對case代碼塊中的影響,輸出值為:" + x ;
default:
x = null;
return "return關(guān)鍵字對case代碼塊中的影響,輸出值為:" + x ;
}
}
}
結(jié)果:
分析:
在本案例測試中,主要是驗證switch()-case語句中return關(guān)鍵字對case代碼塊的影響。
根據(jù)MyEclipse編輯器所顯示的結(jié)果,在Switch-case語句中,當case語句代碼塊中含有return關(guān)鍵字時,其可使程序跳出Switch-case語句。