Java Switch Statement

1306719-20190319115903877-1256924385.png

Java Switch Java Switch Statement

switch语句的执行规则如下

    1、从第一个case开始判断,不匹配则跳到下一个case继续判断;

    2、遇到break则跳出switch语句;

    3、default一般是没有匹配项才执行的,一般是放在switch语句末尾。在如下情况下,它会被执行:一、没有匹配项的时候;二、匹配项最后没有break语句,default跟在这个匹配项后面。

1306719-20190319115921623-1587707169.png

switch表达式支持的数据类型

=>从JDK7开始switch表达式开始支持枚举enum、String以及包装类

  • byte and Byte
  • short and Short
  • char and Character
  • int and Integer
  • enum
  • String

switch语句注意事项

  • switch(表达式)中表达式的返回值必须是下述几种类型之一:byte,short,char,int,枚举enum,String以及对应的包装类(Byte、Short、Character、Integer);

  • case子句中的值必须是常量不允许为变量,而且必须和switch(表达式)中表达式的数据类型一致,且所有case子句中的值应是不同的;

  • break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾;

  • default子句是可选的,当没有匹配的case时,执行default;

  • default子句在最后一行时,下面的break可以省略不写,但如果没有在最后一行default子句下面需要加上break,否则执行完default后会继续执行下面的代码直到遇到break跳出循环;

实践案例

public class SwitchDemo {
    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);
            break;
        case 3:
            System.out.println(3);
        }
    }
}

参考答案

4
1
2
public class SwitchDemo2 {
    public static void main(String[] args) {
        int x = 5;
        switch (x) {
        case 1:
            System.out.println("A");
            break;
        case 2:
            System.out.println("B");
            break;
        default:
            System.out.println("结束");
        case 3:
            System.out.println("C");
            break;
        case 4:
            System.out.println("D");
            break;
        }
    }
}

参考答案

结束
C
public class SwitchDemo3 {
    public static void main(String[] args) {
        System.out.println(getValue(2));
    }

    public static int getValue(int i) {
        int result = 0;
        switch (i) {
        default:
            System.out.println("default");
        case 1:
            result = result + i;
        case 2:
            result = result + i * 2;
        case 3:
            result = result + i * 3;
        }
        return result;
    }
}

参考答案

10
public class SwitchDemo4 {
    public static void main(String[] args) {
        byte a = 4;
        switch (a) {
        default:
            System.out.println("default");
        case 1:
            System.out.println("A");
        case 2:
            System.out.println("B");
        case 3:
            System.out.println("C");
            break;
        case 4:
            System.out.println("D");
        }
    }
}

参考答案

D
public class SwitchDemo5 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            numDays = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            numDays = 30;
            break;
        case 2:
            if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
                numDays = 29;
            else
                numDays = 28;
            break;
        default:
            System.out.println("Invalid month.");
            break;
        }
        System.out.println("Number of Days = " + numDays);
    }
}

参考答案

Number of Days = 29
public class SwitchDemo6 {
    public static void main(String[] args) {
        int number = 20;
        // switch expression with int value
        switch (number) {
        // switch cases without break statements
        case 10:
            System.out.println("10");
        case 20:
            System.out.println("20");
        case 30:
            System.out.println("30");
        default:
            System.out.println("Not in 10, 20 or 30");
        }
    }
}

参考答案

20
30
Not in 10, 20 or 30
public class SwitchDemo7 {
    public static void main(String args[]) {
        // Byte age = 18;
        // Short age = 18;
        // Character age = 18;
        Integer age = 18;
        switch (age) {
        case (16):
            System.out.println("You are under 18.");
            break;
        case (18):
            System.out.println("You are eligible for vote.");
            break;
        case (65):
            System.out.println("You are senior citizen.");
            break;
        default:
            System.out.println("Please give the valid age.");
            break;
        }
    }
}

参考答案

You are eligible for vote.

参考资料

转载于:https://www.cnblogs.com/hglibin/p/10092246.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值