判断某一天是当年的哪一天

需求

输入年份,月份,日,判断这一天是这一年的第几天?(闰年的2月份为29天,平年为28天)

代码

import java.util.*;

public class Test {

    /**
     * 能被4整除且不能被100整除或者能被400整除的年份为闰年
     * @param args
     * @author 孙琨
     */

    public static void main(String[] args) {
        System.out.println("请输入年,月,日");
        Scanner sc = new Scanner(System.in);

        while (sc.hasNextInt()) {
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int totalDay = 0;
            for (int i = 1; i < month; i++) {
                totalDay += getMonthDay(year, i);
                System.out.println("前" + i + "月总天数为" + totalDay);
            }
            System.out.println("总天数为: " + (totalDay + day));
        }

    }

    public static int getMonthDay(int year,int month){
        boolean flag = isLeapYear(year);
        if (month == 2) {
            if(flag == true) {
                return 29;
            } else {
                return 28;
            }
        } else if (month==4 || month==6 || month==9 || month==11) {
            return 30;
        } else {
            return 31;
        }
    }

    public static boolean isLeapYear(int year){
        if ((year%4==0 && year%100!=0) || (year%400==0)) {
            return true;
        } else {
            return false;
        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值