计算日期到天数转换

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

计算日期到天数转换


题目描述

根据输入的日期,计算是这一年的第几天。。

测试用例有多组,注意循环输入

输入描述:
输入多行,每行空格分割,分别是年,月,日

输出描述:
成功:返回outDay输出计算后的第几天;
失败:返回-1

示例1
输入
2012 12 31
输出
366

题目来源:牛客网


解题过程

解题思路

分清闰年,各个月的天数即可方便地计算出天数。

import java.util.*;

public class Main{
    public static void main(String [] args){
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String str = scanner.nextLine();
            String [] arr = str.split(" ");
            int year = Integer.parseInt(arr[0]);
            int month = Integer.parseInt(arr[1]);
            int day = Integer.parseInt(arr[2]);

            int count_day = 0;
            for(int i = 1; i < month; i++){
                if(i == 4 || i == 6 || i == 9 || i == 11){
                    count_day += 30;
                }else if(i == 2){
                    if(year % 4 ==0){
                        count_day += 29;
                    }else{
                        count_day += 28;
                    }
                }else{
                    count_day += 31;
                }
            }
            count_day += day;
            System.out.println(count_day);
        }
    }
}

总结

暂时没有总结,待续。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值