先看一段代码:
public class Test{ public static void main(String[] args){ System.out.println("2 = "+ toNumberCase(2)); } } public static String toNumberCase(int n){ String str = ""; switch(n){ case 0: str = "我是0"; case 1: str = "我是1"; case 2: str = "我是2"; case 3: str = "我是3"; case 4: str = "我是4"; case 5: str = "我是5"; case 6: str = "我是6"; case 7: str = "我是7"; case 8: str = "我是8"; case 9: str = "我是9"; } return str; }
结果是 "2 = 我是2" ?其实不是,结果是
先来分析一下:
由于每个case语句后面少加了break关键字。程序从”case 2"后面的语句开始执行,直到找到break语句结束,可惜的是我们的程序中没有break语句,
于是在程序执行的过程中,str的赋值语句会执行多次,从等于"我是0"、等于"我是1”...等于"我是9",Switch语句执行结束了。于是结果就是如此了。
结论:switch-case语句,如果在每个case语句后面少加了break关键字。程序从该case分支继续执行下一个分支,直到遇见break后或执行完最后一个分支,switch语句执行结束。记住在case语句后面随手写上break语句,养成良好的习惯。
PS:对于此类问题,还有一个简单的解决办法:修改Eclipse的警告级别。Performaces->Java->Compiler->Errors/Warnings->Potential Programming->problems,然后修改'switch' case fall-through为Error级别,你如果没有在case语句中加入break,Eclipse会直接报错。
补充:defalut放在不同位置,对结果的影响
先看几个例子:
例子1:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ default : str = "我是default"; case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; } return str; } }
结果:
例子2:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; default : str = "我是default"; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; } return str; } }
结果:
例子3:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; default : str = "我是default"; } return str; } }
结果:
前3个例子,我把default放在不同的位置, 但是没有加break
接下来再看几个例子:
例子4:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ default : str = "我是default"; break; case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; } return str; } }
结果:
例子5:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; default : str = "我是default"; break; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; } return str; } }
结果:
例子6:
package com.test; public class testbreak { public static void main(String[] args) { System.out.println("10 = "+ toNumberCase(10)); } public static String toNumberCase(int n){ String str = ""; switch(n){ case 0: str = "我是0"; break; case 1: str = "我是1"; break; case 2: str = "我是2"; break; case 3: str = "我是3"; break; case 4: str = "我是4"; break; case 5: str = "我是5"; break; case 6: str = "我是6"; break; case 7: str = "我是7"; break; case 8: str = "我是8"; break; case 9: str = "我是9"; break; default : str = "我是default"; break; } return str; } }
结果:
再看这3个例子,每个都加break
总结:
基础逻辑都是:default的运用,是当switch语句里,所有的case语句都不满足条件时,则执行default语句
在这里我们要分几种情况讨论:
default在switch开头:
若所有case都不满足条件,则执行default语句,并执行default语句之后的case语句,直到break或结束
default在switch中间:(同上)
若所有case都不满足条件,则执行default语句,并执行default语句之后的case语句,直到break或结束
default在switch末尾:
若所有case语句都不满足条件,则执行default语句,结束;若有case满足,则执行case语句直到遇到break或switch语句结束,所以default在最后一行时break可以省略不写(但是不建议省略,以求严谨)