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

计算日期到天数转换

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

主要问题在于:

1.通过年份,判断是否为闰年;

2.通过月份,判断是否为31天;

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner  scan=new Scanner(System.in);
        int year=scan.nextInt();
        int month=scan.nextInt();
        int day=scan.nextInt();

        //准备map
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=1;i<=12;i++){
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
                map.put(i,31);
            }else{
                map.put(i,30);
            }
        }
        //二月的天数的判断
        if(year%4==0&&year%100!=0){
            map.put(2,29);
        }else if(year%100==0&&year%400==0){
            map.put(2,29);
        }else{
            map.put(2,28);
        }

        int count=0;
        //判断第几天
        for(int i=1;i<month;i++){
            count+=map.get(i);
        }
        count+=day;

        //输出
        System.out.println(count);
    }
}
全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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