【问题标题】:Get the nth day of a year获取一年中的第 n 天
【发布时间】:2019-09-27 17:20:39
【问题描述】:

对于家庭作业,我想在 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 日期操作类,如 Calendar, 日期等!

我是初学者,所以希望有人可以帮助我。 这是我到目前为止的代码:

    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: 包;

    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];
      }
    }

【问题讨论】:

  • 查看了LocalDate.of(year, month, day).getDayOfYear()?还是必须重新发明轮子?
  • @ernest_k 我添加了一些新信息。但是我不允许使用 Date 类等。所以我猜它必须发生在 for 循环中..
  • 您需要找到完整格式的日期,即 dd-mm-yy 的天数?
  • @singhV 是的,比如一月一日的数字总是1,闰年的三月一日的数字是61,非闰年是60 .但我已经知道什么时候是闰年。我只需要一个计算天数的for循环。我希望我解释得足够好
  • 请注明您可以使用什么,以便我们根据您的要求回答。

标签: java for-loop days dayofmonth


【解决方案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

【讨论】:

  • 是的,我也认为它必须是一个 int ,但出于某种原因,我的教授把它变成了一个月。将整个代码放在这里是一个想法吗?
  • @mathew 可能您的教授希望您使用 Java Enum for Month,请参见此处:docs.oracle.com/javase/8/docs/api/java/time/Month.html
  • @mathew 但想法是一样的,只是在我有“month-1”的地方你会用“month.minus(1)”替换
  • 类似:for (int i = month.minus(1);i>=1;i--)。但我得到一个错误。
  • 我在我的第一个问题中添加了教授预定义的 Month 类。加上我的整个代码,我觉得这部分很难......
【解决方案2】:
package SO;

import java.time.Month;

public class test {
    //below array contain number of total days in that month(non leap year).
    static int arrTotal[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    public static void main(String[] args) {

        System.out.println(dayNumberInYear(3, Month.MARCH, 2019));
    }

    static int dayNumberInYear(int day, Month month, int year) {
        int dayNumberInYear = day + numberOfDaysInMonth(year, month);

        return dayNumberInYear;
    }

    private static int numberOfDaysInMonth(int year, Month month) {
        int add = 0;

        if (year % 4 == 0)
            add++;

        int a = month.getValue();
        return arrTotal[(a - 2)] + add;
    }
}

【讨论】:

  • 检查这个解决方案。如果有任何问题,请在此处发表评论。
  • 非常感谢!但是我也不允许使用数组......是的,我还在上课,所以我必须在接下来的几周内学习这些东西,所以我已经不允许使用它了。但是非常感谢您花时间!我真的认为它必须是一个 for 循环或一个 while 循环。我还在上面的评论中添加了一个月中天数的函数。我希望你仍然可以帮助我,尽管有一些限制!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多