Java: How many Sundays fell on the first number during the twentieth century (from January 1, 1901 to December 31, 2000)?

I am new to programming and Java, and I am trying to solve the following problem: how many Sundays fell on the first number during the twentieth century (from January 1, 1901 to December 31, 2000)?

Here is my code:

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);

If I print the results, it seems to work fine.

My result is 443, but the correct answer is 171. What am I doing wrong?

Thank!

+6
source share
7 answers

I see some errors:

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

A simple solution:

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);
}
+4
source

, 443 - . , , , , - .

, .

:

  • 1- .
  • , .

, , .

// 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() == JANUARY date1.getDayOfMonth() == 1. , , , . 40 .


Java-TimeStream, :

YearMonthStream
    .from(YearMonth.of(1901, 1))
    .until(YearMonth.of(2000, 12))
    .stream()
    .map(ym -> ym.atDay(1).getDayOfWeek())
    .filter(DayOfWeek.SUNDAY::equals)
    .count();
+12

, , YearMonth, , 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
}
+6

, , for . . :

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

, count, . , , - .

, , .

+3

.

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

443 - , , :

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

Sugestion: 1 12 :

    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

1901 2000 , , , 1901 2000 .

0

Source: https://habr.com/ru/post/1679821/


All Articles