黑马程序员技术交流社区
标题: 关于switch的用法,看完这些例子包你会 [打印本页]
作者: 燃烧的灵魂 时间: 2015-7-2 18:14
标题: 关于switch的用法,看完这些例子包你会
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
case 7:
System.out.println("7成功");
case 8:
System.out.println("08成功");
default:
System.out.println("都失败");
}
}
}运行结果:08成功 都失败
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
case 7:
System.out.println("7成功");
default:
System.out.println("都失败");
}
}
}运行结果:都失败
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 8:
System.out.println("08成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
case 7:
System.out.println("7成功");
default:
System.out.println("都失败");
}
}
}运行结果:08成功
5成功
6成功
7成功
都失败
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 8:
System.out.println("08成功");
break;
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
case 7:
System.out.println("7成功");
default:
System.out.println("都失败");
}
}
}运行结果:08成功
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 8:
System.out.println("08成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
break;
case 7:
System.out.println("7成功");
default:
System.out.println("都失败");
}
}
}运行结果:08成功
5成功
6成功
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
default:
System.out.println("都失败");
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 8:
System.out.println("08成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
break;
case 7:
System.out.println("7成功");
}
}
}运行结果:08成功
5成功
6成功
class SwitchDemo {
public static void main(String[] args) {
int x=8;
switch(x)
{
default:
System.out.println("都失败");
case 0:
System.out.println("0成功");
case 1:
System.out.println("1成功");
case 2:
System.out.println("2成功");
case 3:
System.out.println("3成功");
case 4:
System.out.println("4成功");
case 5:
System.out.println("5成功");
case 6:
System.out.println("6成功");
break;
case 7:
System.out.println("7成功");
}
}
}运行结果:都失败
0成功
1成功
2成功
3成功
4成功
最后再加一句,JDK1.7之后增加的字符串类型也和上面一样哦。
作者: keto 时间: 2015-7-2 19:33
case穿透,break中断、、、、、、、、
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |