Questions icon indicating copy to clipboard operation
Questions copied to clipboard

Java programming course I MOOC Helsinki, question about leap year

Open Willemijn83 opened this issue 1 year ago • 0 comments

Hi, as a beginner I've started working on the MOOC course Java Programming I.

However I'm stuck on the question about leap years. According to the instruction: 'A year is a leap year if it is divisible by 4. However, if the year is divisible by 100, then it is a leap year only when it is also divisible by 400.' I can find code on the internet to solve this, but the course specifically asks to 'start building the program from a situation in which you can be certain that the year is not a leap year.' I think this means I should rule out all the situations where I can be certain there's no leap year.

So the proposed code is:

Scanner reader = new Scanner(System.in); int number = Integer.valueOf(reader.nextLine());

if (number % 4 != 0) { System.out.println("The year is not a leap year."); } else if (...) { ... } ...

Now I've tried things such as:

    } else if ((number % 400 != 0) && (number % 100 != 0)) {
        System.out.println("The year is not a leap year.");
    } else if ((number % 400 == 0) && (number % 100 == 0)) {
         System.out.println("The year is a leap year.");
    } else if (number % 4 == 0) {
        System.out.println("The year is a leap year.");
    }
}

}

But this doesn't work. I know my logic if flawed but I'm not sure where to start in order to solve this problem.

Willemijn83 avatar Oct 30 '24 15:10 Willemijn83