2021. 2. 20. 21:27ㆍJava programming
Write a method isLeapYear with a parameter of type in named yaer.
The parameter needs to be greater than or equal to 1 and less than or equal to 9999.
If the parameter is not in that range return false.
Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is a leap year, otherwise return false.
To determine whether a year is a leap year, follow 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). The method isLeapYear needs to return true.
5. The year is ot a leap year (it has 365 days). The method isLeapYear needs to return false.
public class LeapYear {
public static boolean isLeapYear(int year){
if (year >= 1 && year <= 9999) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;}
} else {
return true;}
} else {
return false;}
}
return false;
}
}
처음엔 leap year의 개념도 잘 몰랐다...
아... 윤년이었구나...!
if 구문이 4개나 되는 못생긴 코드지만 나의 논리가 제대로 구현되어 통과된 게 기뻤다!!!
'Java programming' 카테고리의 다른 글
Coding Exercise 6: Equal Sum Checker (0) | 2021.02.24 |
---|---|
Coding Exercise 5: Decimal Comparator (0) | 2021.02.23 |
Coding Exercise 3: Barking Dog (0) | 2021.02.18 |
Coding Exercise 2: MegaBytes Converter (0) | 2021.02.18 |
Coding Exercise 1: Speed Converter (0) | 2021.02.18 |