import java.util.Scanner;

public class Demo16 {
    /*
     * 使用switch选择结构实现判断某年某月某日是这一年的第几天?
     */
    public static void main(String[] args) {
        Demo16 dm = new Demo16();
        Scanner sc = new Scanner(System.in);
        System.out.println(“请输入年月日如:2000 01 01”);
        int year = sc.nextInt();
        int month = sc.nextInt();
        int day = sc.nextInt();
        int sumDay = 0;
        for (int i = 1; i < month; i++) {
            //System.out.println(“getDaysOfMonth:”+i+”\t”+dm.getDaysOfMonth(year,i));
            sumDay+=dm.getDaysOfMonth(year,i);
            //System.out.println(sumDay);
        }
        System.out.println(“该日是该年第”+(sumDay+day)+”天”);
    }

    public int getDaysOfMonth(int year, int month) {
        switch (month) {
        case 1:
            return 31;
        case 2:
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                return 29;
            } else {
                return 28;
            }
        case 3:
            return 31;
        case 4:
            return 30;
        case 5:
            return 31;
        case 6:
            return 30;
        case 7:
            return 31;
        case 8:
            return 31;
        case 9:
            return 30;
        case 10:
            return 31;
        case 11:
            return 30;
        case 12:
            return 31;
        default:
            return 0;
        }
    }
}


版权声明:本文为lmdbhf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lmdbhf/article/details/125871335