SwitchDemo(1).java【输入年份和月份,判断某年某月有多少天】


//课堂习题:输入年份和月份,判断某年某月有多少天
import java.util.Scanner;
public class SwitchDemo{
    public static void main(String [] args){
        Scanner input=new Scanner(System.in);
        System.out.print("请输入需要查询的年份:");
        int year=input.nextInt();
        System.out.print("请输入需要查询的月份:");
        int month=input.nextInt();
        switch(month){
            case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                System.out.println(year+"年"+month+"月有31天。");
                break;//每一个case后需要添加break跳出switch语句
            case 2:
                if(year%4==0&&year%100!=0||year%400==0){
                    System.out.println(year+"年"+month+"月有29天。");
                }else{
                    System.out.println(year+"年"+month+"月有28天。");
                }
                break;//此处break如果省略,将继续向下执行,一直到遇到break为止或者是switch语句结束
            case 4:case 6:case 9:case 11:
                System.out.println(year+"年"+month+"月有30天。");
                break;
            default:
                System.out.println("ERROR!");
                //break;//default可以省略、此处的break也可以省略
        }
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM