switch 语句

分支结构 :switch-case

格式:

  switch(表达式){

  case 常量1:
    执行语句1;
    break;

  case 常量2:
    执行语句1;
    break;

  case 常量3:
    执行语句1;
    break;

    ......

  default:
    执行语句n;
    break;

}

说明:
  1.break可以省略。break作用:用来跳出switch-case结构
  2.default可以省略。default作用 :如果没有任何一个case匹配成功,那么执行default.
default的位置是灵活的
  3.表达式的类型只能是 :byte,short,int,char,String,枚举
  4.case子句中的值必须是常量,不能是变量名或不确定的表达式值;

public class SwitchTest{

    public static void main(String[] args){
    
        
        int num = 12;

        switch(num){
        //default的位置是灵活的
        default: //default可以省略
            System.out.println("other");
            //break;
        case 1:
            System.out.println(1);
            //break;
        case 2:
            System.out.println(2);
            //break;
        case 3:
            System.out.println(3);
            //break;
        case 5:
            System.out.println(5);
            //break;
        
        }
        


        /*
        编译错误因为表达式的类型只能是 :byte,short,int,char,String,枚举
        switch(true){
        
        case true:
            System.out.println("true");
            break;
        case false:
            System.out.println("false");
            break;

        }
        */

        System.out.println("程序结束了");
    
    }
}
/*

2.对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
*/

public class SwitchTest2{

    public static void main(String[] args){
    
        int score = 56;

        switch(score / 60){
        
        case 0:
            System.out.println("不合格");
            break;
        case 1:    
            System.out.println("合格");
            break;
        }
    }
}
/*

    一 编写程序:从键盘上输入2019年的“month”和“day”,
        要求通过程序输出输入的日期为2019年的第几天。


    二 从键盘分别输入年、月、日,判断这一天是当年的第几天
 
    注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
    或
       2)可以被400整除

*/
import java.util.Scanner;
public class SwitchTest3{

    public static void main(String[] args){
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = sc.nextInt();
        System.out.println("请输入月份");
        int month = sc.nextInt();
        System.out.println("请输入几号");
        int day = sc.nextInt();

        //当年的第几天
        int sumDay = 0;

        switch(month){
        /*
        case 1:
            sumDay = day;
            break;
        case 2:
            sumDay = 31 + day;
            break;
        case 3:
            sumDay = 31 + 28 + day;
            break;
        case 4:
            sumDay = 31 + 28 + 31 +  day;
            break;
        */
        case 4:
            sumDay += 31;
        case 3:
            /*
                注:判断一年是否是闰年的标准:
               1)可以被4整除,但不可被100整除
            或
               2)可以被400整除
            */
            if((year % 400 == 0 ) ||  (year % 4 == 0 && year % 100 != 0)){
                sumDay += 29;
            }else{
                sumDay += 28;
            }
            
        case 2:
            sumDay += 31;
            
        case 1:
            sumDay += day;
        }

        System.out.println(year + "年" + month + "月" + day + "号是当年的第" + sumDay +  "天");
    
    }
}
原文地址:https://www.cnblogs.com/zmy-520131499/p/11066532.html