Java教程

Java输入年份和月份,判断当前月份有多少天

本文主要是介绍Java输入年份和月份,判断当前月份有多少天,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 @Test
    //输入年份和月份,判断当前月份有多少天
    public void test8(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要查询的年份:");
        int year = scanner.nextInt();
        System.out.println("请输入您要查询的月份:");
        int month =scanner.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 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;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println(year+"年"+month+"月共有30天");
                break;
            default:
                break;
        }
    }

这篇关于Java输入年份和月份,判断当前月份有多少天的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!