java基础面试题之:switch的参数类型

1.参数类型

基础数据类型:

(整数):byte,short,int

(字符):char

非基础数据类型:String和枚举类

2.跟break有关的事情:

源代码:

for(int x=0;x<5;x++) {

switch(x) {

case 1:

System.out.println(1);

扫描二维码关注公众号,回复: 7030364 查看本文章

case 2:

System.out.println(2);

case 3:

System.out.println(3);

case 4:

System.out.println(4);

 

 

}

}

System.out.println("break");

for (int x = 0; x < 5; x++) {

switch (x) {

case 1:

System.out.println(1);

break;

case 2:

System.out.println(2);

break;

case 3:

System.out.println(3);

break;

case 4:

System.out.println(4);

break;

 

}

}

 

 

从打印结果上来看:

1

2

3

4

2

3

4

3

4

4

break

1

2

3

4

没有break,不会报错,但是除了第一个符合条件的case有判断功能,后面的case都没有判断功能了,后面的代码会一行一行的打印出来

猜你喜欢

转载自www.cnblogs.com/wowotou-lin/p/11357220.html