亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取一年中的第 n 天

獲取一年中的第 n 天

楊魅力 2023-09-06 15:50:42
對于家庭作業,我想在 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;        }      }
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

看起來您想要的想法是一種按月遞減的遞歸方法。


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;

}

查看完整回答
反對 回復 2023-09-06
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

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;

    }

}


查看完整回答
反對 回復 2023-09-06
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

就這么簡單。


public static String dayNumberInYear(String sDate) {

        //sDate formate 2023-02-20, you can change local date format

        LocalDate localDate = LocalDate.parse(sDate);

        int dayOfYear = localDate.getDayOfYear();

        return dayOfYear > 99 ? "" + dayOfYear : dayOfYear > 9 ? "0" + dayOfYear : "00" + dayOfYear;


    }


查看完整回答
反對 回復 2023-09-06
  • 3 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號