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

import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            int month=in.nextInt();
            int day=in.nextInt();
            TreeMap<Integer,Integer> map=new TreeMap<>();
            map.put(1,31);
            map.put(3,31);
            map.put(4,30);
            map.put(5,31);
            map.put(6,30);
            map.put(7,31);
            map.put(8,31);
            map.put(9,30);
            map.put(10,31);
            map.put(11,30);
            map.put(12,31);
            if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
                map.put(2,29);
                for(Integer m:map.keySet()){
                    if(month>m){
                        day=day+map.get(m);
                    }
                }
            }else{
                map.put(2,28);
                for(Integer m:map.keySet()){
                    if(month>m){
                        day=day+map.get(m);
                    }
                }
            }
            System.out.println(day);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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