java声明一个日期类MyDate

复制代码
使用递归调用求某个整数的阶乘n!

声明一个日期类MyDate,包含如下方法:

* - boolean isLeapYear():判断是否是闰年

* - String monthName():根据月份值,返回对应的英语单词

* - int totalDaysOfMonth():返回这个月的总天数

* - int totalDaysOfYear():返回这一年的总天数

* - int daysOfTheYear():返回这一天是当年的第几数

* 在测试类的main方法中,创建MyDate对象,赋值为当天日期值,调用方法

复制代码
public class MyDate {
 
    //判断是否是闰年
    public boolean isLeapYear(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
 
    //根据月份值,返回对应的英语单词
    public String monthName(int month) {
        switch (month) {
            case 1:
                return "January";
            case 2:
                return "February";
            case 3:
                return "March";
            case 4:
                return "April";
            case 5:
                return "May";
            case 6:
                return "June";
            case 7:
                return "July";
            case 8:
                return "August";
            case 9:
                return "February";
            case 10:
                return "October";
            case 11:
                return "November";
            case 12:
                return "December";
            default:
                return "数据错误";
        }
    }
 
    //返回这个月的总天数
    public int totalDaysOfMonth(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (isLeapYear(year)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }
 
    //返回这一年的总天数
    public int totalDaysOfYear(int year) {
        if (isLeapYear(year)) {
            return 366;
        } else {
            return 365;
        }
    }
 
    //返回这一天是当年的第几天数
    public int daysOfTheYear(int year, int month, int date) {
        int total = 0;
        for (int i = 1; i <= month; i++) {
            total += totalDaysOfMonth(year, month);
        }
        return total += date;
    }
}

public class MyDateTest {
    public static void main(String[] args) {
        MyDate myDate = new MyDate();
 
        System.out.println("请输入年份:");
        Scanner scan1 = new Scanner(System.in);
        int year = scan1.nextInt();
        System.out.println("请输入月份:");
        Scanner scan2 = new Scanner(System.in);
        int month = scan2.nextInt();
        System.out.println("请输入日期:");
        Scanner scan3 = new Scanner(System.in);
        int date = scan2.nextInt();
 
//        System.out.println(year);
//        System.out.println(month);
//        System.out.println(date);
 
        System.out.println("今年是否是闰年:"+myDate.isLeapYear(year));
        System.out.println("这个月对应的英文单词是:"+myDate.monthName(month));
        System.out.println("这个月总共"+myDate.totalDaysOfMonth(year, month)+"天");
        System.out.println("今年的总天数是"+myDate.totalDaysOfYear(year)+"天");
        System.out.println("今天是今年的第"+myDate.daysOfTheYear(year, month, date)+"天");
    }
}
相关推荐
无聊的小坏坏5 分钟前
力扣 239 题:滑动窗口最大值的两种高效解法
c++·算法·leetcode
WanderInk8 分钟前
揭秘Java协变返回类型:让你的API少一点强转,多一点优雅
java·后端
黎明smaly9 分钟前
【排序】插入排序
c语言·开发语言·数据结构·c++·算法·排序算法
心在飞扬15 分钟前
AI开发应用 01-nodejs快速入门
后端·aigc·dnodejs
Ray6619 分钟前
「阅读笔记」elasticSearch 是什么?工作原理是怎么样的?
后端
paopaokaka_luck20 分钟前
基于SpringBoot+Vue的非遗文化传承管理系统(websocket即时通讯、协同过滤算法、支付宝沙盒支付、可分享链接、功能量非常大)
java·数据库·vue.js·spring boot·后端·spring·小程序
YuTaoShao21 分钟前
【LeetCode 热题 100】206. 反转链表——(解法一)值翻转
算法·leetcode·链表
iteye_993922 分钟前
让 3 个线程串行的几种方式
java·linux
辣辣y25 分钟前
python基础day08
开发语言·python
YuTaoShao29 分钟前
【LeetCode 热题 100】142. 环形链表 II——快慢指针
java·算法·leetcode·链表