java switch条件类型_java中switch的用法以及判断的类型有哪些(String\byte\short\int\char\枚举类型)...

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 package codeAnal;

2

3 public class SwitchDemo {

4

5 public static void main(String[] args) {

6 stringTest();

7 breakTest();

8 defautTest();

9 }

10

11 /*

12 * default不是必须的,也可以不写

13 * 输出:case two

14 */

15 private static void defautTest() {

16 char ch = 'A';

17 switch (ch) {

18 case 'B':

19 System.out.println("case one");

20 break;

21 case 'A':

22 System.out.println("case two");

23 break;

24 case 'C':

25 System.out.println("case three");

26 break;

27 }

28 }

29

30 /*

31 * case语句中少写了break,编译不会报错

32 * 但是会一直执行之后所有case条件下的语句,并不再进行判断,直到default语句

33 * 下面的代码输出: case two

34 * case three

35 */

36 private static void breakTest() {

37 char ch = 'A';

38 switch (ch) {

39 case 'B':

40 System.out.println("case one");

41

42 case 'A':

43 System.out.println("case two");

44

45 case 'C':

46 System.out.println("case three");

47 default:

48 break;

49 }

50 }

51

52 /*

53 * switch用于判断String类型

54 * 输出:It's OK!

55 */

56 private static void stringTest() {

57 String string = new String("hello");

58 switch (string) {

59 case "hello":

60 System.out.println("It's OK!");

61 break;

62

63 default:

64 System.out.println("ERROR!");

65 break;

66 }

67 }

68 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值