switch case语句的用法

  1. witch支持部分基本数据类型(primitive data types),如:byte、short、int、long、char;不支持boolean、float、double。

    如图的例子:

    switch case语句的用法
  2. 支持Enum类型、String、和部分基本类型的包装类(如:Character、Byte、Short、Integer);

    如图的例子:

    switch case语句的用法
  3. 3

    break关键字可以结束switch语句,如果没有break,则从符合条件的case开始一直往下执行到switch结束或者遇到break。如图例子,结果为4:

    switch case语句的用法
  4. 4

    可以有多个case的语句。如图:

    switch case语句的用法



5.

switch支持的类型有?

  • Java 7 中加入了对String类型的支持。所以支持的有:char、byte、short、int 和 Character、Byte、Short、Integer 和 String
6.public static void main(String[] args) {   
        int type=4;
        switch (type) {  
        default:  
            System.out.println(4);  
        case 1:  
            System.out.println(1);  
        case 2:  
            System.out.println(2);  
        case 3:  
            System.out.println(3);  
        }  

    }


输出结果  4 1 2 3


没有break就一直输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值