【问题标题】:How to calculate the remaining days in a year from user input如何根据用户输入计算一年中的剩余天数
【发布时间】:2018-10-25 20:00:53
【问题描述】:

我有一个作业问题是这样写的:编写一个程序,提示用户输入一个日期(以三个数字的形式:年月日),并使用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;
}

}

【问题讨论】:

    标签: java


    【解决方案1】:

    我认为使用LocalDateChronoUnit.DAYS.between() 会更容易

    LocalDate date = LocalDate.of(year, month, days);
    LocalDate newYearsDay = LocalDate.of(year + 1, 1,  1);
    long dif = ChronoUnit.DAYS.between(date, newYearsDay);
    
    System.out.println("There are " + dif + " day(s) until New Year.");
    

    【讨论】:

      【解决方案2】:

      您的问题可能与“如何计算一段时间内的天数?”重复。这里: How to calculate the number of days in a period? 您可以使用 Period.between(date, newYearDate) 并将其转换为天数或 ChronoUnit.DAYS.between(date, newYearDate) 来实现您想要的。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          您应该为此目的使用日期感知类,但只是为了好玩,更重要的是为了教育目的,您可能可以像这样开始:

          int remainingDays = getDays(month, year) - days + 1;
          for(int currentMonth = month + 1; currentMonth <= 12; currentMonth++) {
             remainingDays += getDays(year, currentMonth);
          }
          

          【讨论】:

          • 对于作业问题,最好不要直接给出答案,而是对目前所做的工作给予提示和改进
          • 是的,我唯一改变的是在循环中你将 1 添加到月份变量(必须删除它)并且它工作得很好,谢谢:)
          【解决方案5】:

          我建议您为问题的每个部分都使用命名良好的辅助方法。这将使您的代码更易于理解和使用。

          例如:

          public static boolean isLeapYear(int year) {
              return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
          }
          
          public static int daysInMonth(int year, int monthNum) {
              int[] days = {9999, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
              if (monthNum == 2 && isLeapYear(year)) {
                  return 29;
              }
              return days[monthNum];
          }
          

          顺便说一句,我调用参数monthNum 而不是month 的原因是为了更清楚地表明它不是从零开始的,一月= 0 等。我还将9999 放入数组中对于元素0,以确保如果我们确实犯了这个错误,它将在计算中脱颖而出。

          对于循环,您需要从指定月份到年底for (int m = monthNum; m &lt;= 12; m++) { ... },保持每个月份的天数总和,然后减去当前月份的指定日期。

          【讨论】:

          • 来吧伙计。你也回答了......用文字! :D
          • 我没有给出实际的代码来做这件事。我可能在我的回答中给出了太多。关键是你想给出提示而不实际为他们做功课。
          猜你喜欢
          • 2016-11-15
          • 2014-11-26
          • 2014-06-11
          • 1970-01-01
          • 1970-01-01
          • 2012-08-05
          • 2013-06-26
          • 2010-11-13
          相关资源
          最近更新 更多