题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] date = sc.nextLine().split("\\s+");
        int year = Integer.parseInt(date[0]);
        int month = Integer.parseInt(date[1]);
        int day = Integer.parseInt(date[2]);
        // 判断是否是闰年
        boolean isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
        int ans = 0;
        switch (month - 1) {
            case 11:
                ans += 30;
            case 10:
                ans += 31;
            case 9:
                ans += 30;
            case 8:
                ans += 31;
            case 7:
                ans += 31;
            case 6:
                ans += 30;
            case 5:
                ans += 31;
            case 4:
                ans += 30;
            case 3:
                ans += 31;
            case 2:
                if (isLeap) {
                    ans += 29;
                } else {
                    ans += 28;
                }
            case 1:
                ans += 31;
                break;
            default:
                ans += day;
        }
        if (month == 1) {
            System.out.println(ans);
        } else {
            System.out.println(ans + day);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务