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

计算日期到天数转换

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            String str = sc.nextLine();
            String[] split = str.split(" ");
            int year = Integer.parseInt(split[0]);
            int month = Integer.parseInt(split[1]);
            int day = Integer.parseInt(split[2]);
            int sum = 0;// 共多少天
            // int[] months = {31,29,31,30,31,30,31,31,30,31,30,31};
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                // 闰年,2月为29天
                int[] months = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                for (int i = 0; i < month - 1; i++) {
                    sum += months[i];
                }
                sum += day;
            } else {
                // 平年,2月为28天
                int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                for (int i = 0; i < month - 1; i++) {
                    sum += months[i];
                }
                sum += day;
            }

            System.out.println(sum);
        }
    }
}

全部评论

相关推荐

肖先生~:先别说工资,现在有个工作就不错了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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