You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:
}
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.
The text was updated successfully, but these errors were encountered: