Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java programming course I MOOC Helsinki, question about leap year #534

Open
Willemijn83 opened this issue Oct 30, 2024 · 0 comments
Open

Comments

@Willemijn83
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant