gpt4 book ai didi

java - 如何根据用户输入计算一年中的剩余天数

转载 作者:行者123 更新时间:2023-12-02 01:50:56 26 4
gpt4 key购买 nike

我有一个作业问题,内容如下:编写一个程序,提示用户输入日期(以三个数字的形式:年月日),并使用getDays()方法来帮助计算从给定日期到新年的天数。以下是一些示例运行:

  • 请输入日期:2018 年 12 月 20 日
    距离新年还有 12 天。
  • 请输入日期:1980 年 1 月 5 日
    距离新年还有 362 天。

这是我到目前为止的代码:

public class Lab9Question4 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a date: ");
int year = input.nextInt();
int month = input.nextInt();
int days = input.nextInt();
input.close();
long diff = getDays(year, month) - days;
long days_diff = diff + 1;
System.out.println("There are " + days_diff + " day(s) until New Year.");
}

public static int getDays(int year, int month) {
int number_Of_DaysInMonth = 0;

switch (month) {
case 1:
number_Of_DaysInMonth = 31;
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
number_Of_DaysInMonth = 31;
break;
case 4:
number_Of_DaysInMonth = 30;
break;
case 5:
number_Of_DaysInMonth = 31;
break;
case 6:
number_Of_DaysInMonth = 30;
break;
case 7:
number_Of_DaysInMonth = 31;
break;
case 8:
number_Of_DaysInMonth = 31;
break;
case 9:
number_Of_DaysInMonth = 30;
break;
case 10:
number_Of_DaysInMonth = 31;
break;
case 11:
number_Of_DaysInMonth = 30;
break;
case 12:
number_Of_DaysInMonth = 31;

}
return number_Of_DaysInMonth;
}
}

我知道我需要在某处使用循环,以便获取用户输入后的剩余天数,并将它们添加到日期差中以获得直到新的一年的天数,但我不确定在哪里插入循环以及其中应该包含的内容。如有任何帮助,我们将不胜感激:)

更新:感谢所有快速回复的人,此代码现在可以完美运行!新代码如下,供将来可能需要它的其他人使用:

public class Lab9Question4 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a date: ");
int year = input.nextInt();
int month = input.nextInt();
int days = input.nextInt();
input.close();
int remainingDays = getDays(month, year) - days + 1;
for (int currentMonth = month; currentMonth <= 12; currentMonth++) {
remainingDays += getDays(year, currentMonth);
}

System.out.println("There are " + remainingDays + " day(s) until New Year.");
}

public static int getDays(int year, int month) {
int number_Of_DaysInMonth = 0;

switch (month) {
case 1:
number_Of_DaysInMonth = 31;
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
number_Of_DaysInMonth = 31;
break;
case 4:
number_Of_DaysInMonth = 30;
break;
case 5:
number_Of_DaysInMonth = 31;
break;
case 6:
number_Of_DaysInMonth = 30;
break;
case 7:
number_Of_DaysInMonth = 31;
break;
case 8:
number_Of_DaysInMonth = 31;
break;
case 9:
number_Of_DaysInMonth = 30;
break;
case 10:
number_Of_DaysInMonth = 31;
break;
case 11:
number_Of_DaysInMonth = 30;
break;
case 12:
number_Of_DaysInMonth = 31;

}
return number_Of_DaysInMonth;
}

}

最佳答案

我建议你查看LocalDate的文档类(class)。

.until() 方法应该适合您的情况。您不必手动计算。

关于java - 如何根据用户输入计算一年中的剩余天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52997201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com