0

so the code works but some years which should be leap years are not like 2008 is showing its not a leap year which it was.

//is leap year if...
      if ((whichYear % 4 == 0) && (whichYear % 100 == 0) && (whichYear % 400 == 0))      
         {
          isLeapYear = true;
          daysLeftInYear = 366;                          
          System.out.println("true " + daysLeftInYear);
         } 

         else 
              {
               isLeapYear = false;
               daysLeftInYear = 365;
               System.out.println("false " + daysLeftInYear);
              }
  • 6
    Your current logic says that a leap year must be divisible by 4 AND 100 AND 400. That's probably not what you meant. – assylias Feb 04 '20 at 14:17
  • 1
    A number that is devisable by 100 is surely also devisable by 4, isn´t it? So your first check is equivalent to just `if(whichYear % 400 == 0)`. Thus your entrie defintion of a leapyear is "every year that is divable by 400". – MakePeaceGreatAgain Feb 04 '20 at 14:18
  • my teacher wants the program to go through these steps // 1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5. // 2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4. // 3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5. // 4. The year is a leap year (it has 366 days). // 5. The year is not a leap year (it has 365 days). – data10019 Feb 04 '20 at 14:18
  • Leap Years are years which are divisible by 4.Except for century years which must be divisible by 400. Your logic used in the ' if ' condition is wrong. – Navidk Feb 04 '20 at 14:18
  • 1
    think i figured it out : is a leap year if it can be evenly divided by 4; If the year can be evenly divided by 100, it is NOT a leap year, unless; The year is also evenly divisible by 400. Then it is a leap year. – data10019 Feb 04 '20 at 14:22
  • While you can do this with a one line expression, you may find it easier to write some if/else statements that exactly mimic what your teacher asked for. You can even put comments like `// Step 3` in your code. It may look longer, but it won’t be any slower than the one-line version. – VGR Feb 04 '20 at 15:32

2 Answers2

0

The easiest way is to just break it down into two possibilities.

If (year % 100 == 0) {
  // if century year
} else {
  // if not century year.
}

The rest is up to you.



WJS
  • 36,363
  • 4
  • 24
  • 39
0

Well of course it won't work with 2008 :) Because you defined leap years as being divisible by 4 AND divisible by 100 AND divisible by 400 :) With this definition only every 400th year will be a leap year, for example 1600.

The correct definition of a leap year is: Divisible by 4 AND if it is divisible by 100, then also divisible by 400. For example 1700 is not a leap year, but 1200 is.

So in terms of Java this would lead to:

int year; // initialize it with some year
boolean leapYear = year % 4 == 0 && ((year % 100 == 0) == (year % 400 == 0))
DanielBK
  • 892
  • 8
  • 23