2017-06-21 59 views
4

我是新來的編程和Java,我試圖解決以下問題: 20世紀期間(1901年1月1日至2000年12月31日),這個月的第一個月有多少個星期日下降?JAVA - 二十世紀(1901年1月1日至2000年12月31日)這個月的第一個月有多少個星期日下降?

這裏是我的代碼:

public class test { 
    public static void main(String[] args) { 
     int count, sum = 0;   
     for (int i = 1901; i < 2001; i++) { 
      LocalDate test = LocalDate.of(i,1,1); 
      sum += test.lengthOfYear(); 
     } 
     for (int i = 1; i < sum; i++) { 
      LocalDate date1 = LocalDate.of(1901,1,1); 
      date1 = date1.plusDays(i); 
     if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) { 
      count++; 
      } 
     } 
     System.out.println(count); 
    } 
} 

如果我打印結果,它似乎是工作的罰款。

我的結果是443,但正確的答案是171.我做錯了什麼?

謝謝!

回答

3

我看到一些錯誤:

public static void main(String[] args) { 
    int count, sum = 0;   
    for (int i = 1901; i < 2001; i++) { // There is a mistake here, I dont know what you want to compute in this loop! 
     LocalDate test = LocalDate.of(i,1,1); 
     sum += test.lengthOfYear(); 
    } 
    for (int i = 1; i < sum; i++) { 
     LocalDate date1 = LocalDate.of(1901,1,1); // There is a mistake here, date1 must be outside of this loop 
     date1 = date1.plusDays(i); // There is a mistake here, plusDays why?? 
    if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) { // There is a mistake here, why are you cheking this: date1.getMonth() == JANUARY ? 
     count++; 
     } 
    } 
    System.out.println(count); 
} 

一個簡單的辦法:

public static void main(String[] args) { 
    int count = 0; 
    LocalDate date1 = LocalDate.of(1901, Month.JANUARY, 1); 
    LocalDate endDate = LocalDate.of(2001, Month.JANUARY, 1); 
    while (date1.isBefore(endDate)) { 
     date1 = date1.plusMonths(1); 
     if (date1.getDayOfWeek() == DayOfWeek.SUNDAY) { 
      count++; 
     } 
    } 
    System.out.println(count); 
} 
+0

謝謝!我沒有正確理解所要求的。愚蠢的我:)我沒有想到使用isBefore ...看起來好多了。 – GeoTeo

11

我懷疑443是二十世紀一月份的星期日總數。發生這種情況是因爲你走過了二十世紀所有可能的日子,然後檢查當前月份是否爲一月份,以及當前日期是否爲星期日。

這不是你想要的。

我會用不同的方法:

  • 走過去每一年的每個月的第一天。
  • 然後檢查它是否是星期天。

該代碼可能會快得多。

// Each year 
for (int y = 1901; y < 2001; y++) { 
    // Each month of the year 
    for (int m = 1; m <= 12; m++) { 
     if (LocalDate.of(y, m, 1).getDayOfWeek() == DayOfWeek.SUNDAY) { 
      count++; 
     } 
    } 
} 

PS:您的代碼將是正確的,如果你改變了date1.getMonth() == JANUARYdate1.getDayOfMonth() == 1。然而,這是非常低效的,因爲它會檢查二十世紀的每一天,而它只需要檢查每個月的第一天。上述代碼在我的機器上快大約40倍。

+1

或者,您可以使用'LocalDate.plusMonths'在像http://ideone.com/QW2iC3這樣的單個循環中執行此操作。 –

3

您首先找到您檢查期間的天數,然後啓動for循環以運行該期間。到現在爲止還挺好。但是,你的增加計條件是錯誤的:

if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY)

這意味着,當你遍歷每一天,你增加count如果該日期是一月一個星期天。你不檢查那個星期天是否是一月的第一個星期,而且你沒有計算二月到十二月的任何事情。

你應該檢查它是月份和星期天的第一天,而不是年和週日的第一個月。

6

除了已經被標記錯誤,你可以重新考慮你的設計,並使用the YearMonth class這似乎是你的使用情況比LocalDate更適合:

public static void main(String[] args) { 
    YearMonth start = YearMonth.of(1901, 1); 
    YearMonth end = YearMonth.of(2000, 12); 

    int count = 0; 
    for (YearMonth ym = start; !ym.isAfter(end); ym = ym.plusMonths(1)) { 
    //is first day of month a sunday? 
    if (ym.atDay(1).getDayOfWeek() == SUNDAY) count ++; 
    } 

    System.out.println(count); //171 
} 
2

下面的代碼應該輸出正確的值。

public static void main(String[] args) { 
    int count = 0, sum = 0; 
    for (int i = 1901; i < 2001; i++) { 
     LocalDate test = LocalDate.of(i, 1, 1); 
     sum += test.lengthOfYear(); 
    } 

    for (int i = 1; i < sum; i++) { 
     LocalDate date1 = LocalDate.of(1901, 1, 1); 
     date1 = date1.plusDays(i); 
     if (date1.getDayOfMonth() == 1 && date1.getDayOfWeek() == java.time.DayOfWeek.SUNDAY) { 
      count++; 
     } 
    } 

    System.out.println(count); 
} 

需要注意的是:

  1. 你並不需要檢查月份是一月,因爲你的需求爲大約每月的任何一日。
  2. 您還必須確保當天是當月的第一天。
0

443是星期日的數月,如果你想在第一個月的星期天,你必須將您的代碼更改爲:

if(date1.getDayOfMonth() == 1 && date1.getDayOfWeek() == SUNDAY) { 
      count++; 
      } 
     } 

Sugestion:instead of走過去每一年的每一天,你可以在1個月只需添加到日期UTIL12個月:

for (int i = 1901; i < 2001; i++) { 
     for(int mon =0; mon<12; mon++){ 
      LocalDate date1= LocalDate.of(i,mon,1); 
      if(date1.getDayOfWeek() == SUNDAY) { 
      count++; 
      } 
     } 
    } 
0

你只考慮月份一個月2001至00年,這是不正確,你應該考慮是否第一天從1901年到2000年的每個月都是星期日或者不星期一。

相關問題