java - 获取一年中的第 n 天
问题描述
对于家庭作业,我想在 Jave 程序中计算日期在一年中的第 n 天。
因此,用户给出一个日期,然后它会告诉它一年中有多少天。所以 2019 年 1 月 1 日是第一天。我已经有一个函数可以给出一个月的天数。此函数还考虑闰年。所以我只需要一个函数来返回一年中的天数。我的想法,我必须做,但它不起作用,因为我不能减少一个月:
static int dayNumberInYear(int day, Month month, int year)
{
while(month!=Month.JANUARY)
{
int dayNumberInYear=dayNumberInYear+numberOfDaysInMonth(year,month);
Month month = month(-1);
}
return dayNumberInYear(day,month,year)+day;
}
我知道这是不对的,所以我希望有人能帮助我。我认为for循环更好,但我不知道如何。第一行,static int dayNumberInYear(int day, Month month, int year)
我不能改变它,所以它必须是第一行。
我不允许使用 java JRE 日期操作类,如日历、日期等!
我是初学者,所以希望有人可以帮助我。这是我到目前为止的代码:
package ;
import java.util.Scanner;
public class Easter
{
public static void main(String[] arguments)
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the day month and year with spaces in between:");
int day=scanner.nextInt();
int monthNumber=scanner.nextInt();
Month month=Month.month(monthNumber);
int year=scanner.nextInt();
System.out.println("The month "+month+" has "+numberOfDaysInMonth(year,month)+" days in year "+year);
System.out.println("The number of this date in a year:"+dayNumberInYear(day,month,year));
scanner.close();
}
static boolean isLeapYear(int year)
{
if(year%100==0)
year=year/100;
if(year%4==0)
return true;
return false;
}
static int numberOfDaysInMonth(int year, Month month)
{
switch(month)
{
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
case FEBRUARY:
if(isLeapYear(year))
return 29;
return 28;
default:
return 31;
}
}
static int dayNumberInYear(int day, Month month, int year)
{
while(month!=Month.JANUARY)
{
int dayNumberInYear=dayNumberInYear+numberOfDaysInMonth(year,month);
Month month = month(-1);
}
return dayNumberInYear(day,month,year)+day;
}
}
已经有一个预制类 Month.java: package ;
public enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
public int number()
{
return ordinal()+1;
}
public static Month month(int number)
{
return values()[number-1];
}
}
解决方案
看起来你想要的想法是一种按月递减的递归方法。
static int dayNumberInYear(int day, Month month, int year)
{
if(!month.equals(Month.JANUARY)) {
return day + dayNumberInYear(numberOfDaysInMonth(month.minus(1), year), month.minus(1), year);
} else {
return day;
}
}
请注意,在这种递归方法中,基本情况是一月份,我们只需返回一月份的当前日期。否则,我们添加当月的日期,然后添加上个月的所有日期。
它也可以作为一个for循环来完成。
static int dayNumberInYearByLoop(int day, Month month, int year) {
int totalDays = day;
for (int i = month.getValue();i>1;i--) {
totalDays += numberOfDaysInMonth(Month.of(i), year);
}
return totalDays;
}
你可以在这里弄乱我的代码:https ://repl.it/repls/ConcreteDarkgreenSequence
推荐阅读
- java - 为什么我的数据只在读取时刷新到数据库?
- apache-spark - 没有流时的Spark结构化流窗口
- arrays - 喜欢 - 不像 MongoDb 中的系统来查找用户是否喜欢?
- git - qt.qpa.plugin 的 Git Difftool 问题:在“”中找不到 Qt 平台插件“cocoa”
- python - 仅针对一个文件的 Python 命令未找到错误
- angular - spyOn(document, 'getElementById') 升级到 angular 8/"@types/jasmine": "^3.4.0" 后抛出错误
- docker - WORKDIR 作为 VOLUME
- node.js - Parallelshell 模块未在 npm 上运行。可以做什么?
- swift - 如何在 Swift 中替换字符串中的子字符串
- mysql - 是否可以将键用作列,将值用作键/值数据库结构的值?