Java基础-距今天出生了多少天

需求---分析---设计---编码---测试---上线---维护

需求:根用户输入的出生信息:年月日,得出到现在出生了多少天。

    分析                      设计
1. 判断闰年的方法         boolean     isLeaper(int year)
2. 判断月份天数的方法     int     monthDays(int year,int month)
3. 获取系统当前的日期     难点    用日历类  先直接输入今天的年月日
4. 计算                   int     calc(int year,int month,int day)
5. 测试                   void    main()


年算好了

2018年3月3日
2020年3月3日

2020年7月29日

1月1到3月3日有多少天 x
1月1到7月29日有多少天 y

y-x 天
package teacher;

import java.util.Calendar;
import java.util.Scanner;

/**
 * @ClassName TestBorn
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/7/29 0029
 * @Version V1.0
 **/
public class TestBorn {
    /**
     * 1. 判断闰年的方法
     */
    static boolean isLeaper(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 2. 判断月份天数的方法
     */
    static int monthDays(int year, int month) {
        int days = 31;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            days = 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            days = 30;

        } else if (month == 2) {
            //与年有关//调用判断闰年的方法 方法间的调用
            boolean flag = isLeaper(year);
            if (flag) {
                days = 29;
            } else {
                days = 28;
            }
        }

        return days;
    }

    /**
     * 3. 获取系统当前的日期     难点  直接输入今天的年月日
     */
    //getSystemDate()

    /**
     * 4. 计算年
     *
     * @param bornYear 出生年
     * @param nowYear  当前年
     * @return 出生年到当前年的天数
     */
    static int calcYearDays(int bornYear, int nowYear) {
        //1.先算年的天数
        //假调你是1998年出生,到今天2020年,总共多少年呢
        //2020-1998=22年
        int yearDays = 0;
        for (int i = bornYear; i < nowYear; i++) {
            if (isLeaper(i)) {
                yearDays += 366;
            } else {
                yearDays += 365;
            }
        }
        return yearDays;
    }
    /**
     * 计算l月1日到当前月的天数
     * @param year 年
     * @param month 月
     * @param days  日
     * @return 从1月到这个月的天数
     */
    static int allMonthDays(int year, int month, int days) {
        int monthDays = 0;
        for (int i = 1; i < month; i++) {
            int day = monthDays(year, i);
            monthDays += day;
        }
        return monthDays + days;
    }

    /**
     * 5. 测试
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入你出生的年:");
        int bornYear = input.nextInt();
        System.out.print("请输入你出生的月:");
        int bornMonth = input.nextInt();
        System.out.print("请输入你出生的日:");
        int bornDays = input.nextInt();
        //日历类
        Calendar c = Calendar.getInstance();
        int nowYear = c.get(Calendar.YEAR);
        int nowMonth = c.get(Calendar.MONTH) + 1;
        int nowDays = c.get(Calendar.DAY_OF_MONTH);
        //计算
        int yearDays = calcYearDays(bornYear, nowYear);

        //计算1月1日到3月3日的总天数
        int bornMonthDays = allMonthDays(bornYear, bornMonth, bornDays);
        //计算1月1日到7月29日
        int nowMonthDays = allMonthDays(nowYear, nowMonth, nowDays);
        //出生天数
        int myDays = nowMonthDays - bornMonthDays + yearDays;
        System.out.println("你在" + bornYear + "年" + bornMonth + "月" + bornDays + "日出生,");
        System.out.println("距今已经:" + myDays + "天");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值