Java based learning: 2.switch statement Precautions

A, switch statements use Precautions

switch statement format is as follows

switch (switch表达式){
            case 值1: 语句1;break;
            case 值2: 语句2;break;
            ...
            case 值N: 语句N;break;
            default:  默认情况下执行的语句
        }

Note the following issues:

  1. switch expression must be able to calculate a char, byte, short, int, or a String value, float, and double, and other types are not allowed;
  2. Value 1, value 2 and the like are constants;
  3. By default, default is optional;
  4. Fall behavior: If you forget to use the break, once a match which case, it will begin at the match from the case until it encounters a break statement (here not break statement) or the switch statement ends. The following code, the first 1 day to 5 show WEEKDAY, 0 and 6 show weekends.
switch(day){

    case 1:
    case 2:
    case 3:
    case 4:
    case 5:System.out.println("Weekday");break;
    case 0:
    case 6:System.out.println("weekends");

}

 

Published 33 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41623154/article/details/104808417