英文:
The required output is false, true, false, true. My results are false, true, true, true. Is there something missing in my code?
问题
这是情景。
写一个名为isLeapYear的方法,带有一个名为year的int类型参数。
参数的值需要大于等于1且小于等于9999。
如果参数不在该范围内,则返回false。
否则,如果在有效范围内,计算年份是否是闰年,如果是闰年则返回true,否则返回false。
要判断一个年份是否是闰年,请按照以下步骤进行:
- 如果年份可以被4整除,转到步骤2。
- 否则,转到步骤5。
- 如果年份可以被100整除,转到步骤3。
- 否则,转到步骤4。
- 如果年份可以被400整除,转到步骤4。
- 否则,转到步骤5。
- 该年是闰年(有366天)。方法isLeapYear需要返回true。
- 该年不是闰年(有365天)。方法isLeapYear需要返回false。
以下年份不是闰年:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
因为它们可以被100整除,但不能被400整除。
以下年份是闰年:
1600, 2000, 2400
因为它们既可以被100整除,又可以被400整除。
输入/输出示例:
-
isLeapYear(-1600); → 应返回false,因为参数不在范围内(1-9999)
-
isLeapYear(1600); → 应返回true,因为1600是闰年
-
isLeapYear(2017); → 应返回false,因为2017不是闰年
-
isLeapYear(2000); → 应返回true,因为2000是闰年
这是我的代码:
public static boolean isLeapYear(int year) {
boolean result;
if (year >= 1 && year <= 9999) {
result = true;
} else {
result = false;
}
boolean step;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
step = result;
}
} else {
step = result;
}
} else {
step = false;
}
return step;
}
英文:
This is the scenario.
Write a method isLeapYear with a parameter of type int named year.
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:
- If the year is evenly divisible by 4, go to step
- Otherwise, go to step 5.2. If the year is evenly divisible by 100, go to step
- Otherwise, go to step 4.3. If the year is evenly divisible by 400, go to step
- Otherwise, go to step 5.4. The year is a leap year (it has 366 days). The method isLeapYear needs to return true.
- The year is not a leap year (it has 365 days). The method isLeapYear needs to return false.
The following years are not leap years:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
This is because they are evenly divisible by 100 but not by 400.
The following years are leap years:
1600, 2000, 2400
This is because they are evenly divisible by both 100 and 400.
Examples of input/output:
-
isLeapYear(-1600); → should return false since the parameter is not in range (1-9999)
-
isLeapYear(1600); → should return true since 1600 is a leap year
-
isLeapYear(2017); → should return false since 2017 is not a leap year
-
isLeapYear(2000); → should return true because 2000 is a leap year
This is my code:
public static void isLeapYear(int year) {
boolean result;
if (year >= 1 && year <= 9999) {
System.out.println(result = true);
} else {
System.out.println(result = false);
}
boolean step;
if (year == (year % 4)) {
if (year == (year % 100)) {
step = result;
}
}
if (year == (year % 100)) {
if (year == (year % 400)) {
step = result;
}
}
if (year == (year % 400)) {
step = result;
} else {
step = result;
}
}
答案1
得分: 1
public static void main(String[] args) throws IOException {
isLeapYear(-1600);// → 应返回 false,因为参数不在范围内(1-9999)
isLeapYear(1600); ////→ 应返回 true,因为 1600 是闰年
isLeapYear(2017);// → 应返回 false,因为 2017 不是闰年
isLeapYear(2000);
isLeapYear(99999);
}
public static void isLeapYear(int year) {
// 应在 1 到 9999 之间
// 如果年份可以被 4 整除,进入步骤 2...
// 如果年份可以被 100 整除,进入步骤 3...
// 如果年份可以被 400 整除,进入步骤 4...
// 该年是闰年(有 366 天)。
// 该年不是闰年(有 365 天)。
boolean isLeap = false;
if(year >= 1 && year <= 9999 && year % 4 == 0){
if( year % 100 == 0){
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}else
isLeap = true;
}else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " 是闰年。");
else
System.out.println(year + " 不是闰年。");
}
}
输出
-1600 不是闰年。
1600 是闰年。
2017 不是闰年。
2000 是闰年。
99999 不是闰年。
英文:
I have made some changes and merged ur conditions
public static void main(String[] args) throws IOException {
isLeapYear(-1600);// → should return false since the parameter is not in range (1-9999)
isLeapYear(1600); ////→ should return true since 1600 is a leap year
isLeapYear(2017);// → should return false since 2017 is not a leap year
isLeapYear(2000);
isLeapYear(99999);
}
public static void isLeapYear(int year) {
//Should lie between 1 && 9999
//If the year is evenly divisible by 4, go to step 2. ...
//If the year is evenly divisible by 100, go to step 3. ...
//If the year is evenly divisible by 400, go to step 4. ...
//The year is a leap year (it has 366 days).
//The year is not a leap year (it has 365 days).
boolean isLeap = false;
if(year >= 1 && year <= 9999 && year % 4 == 0){
if( year % 100 == 0){
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}else
isLeap = true;
}else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
output
-1600 is not a Leap Year.
1600 is a Leap Year.
2017 is not a Leap Year.
2000 is a Leap Year.
99999 is not a Leap Year.
答案2
得分: 1
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; // year%400==0 else
}
} else {
return true; // year%100==0 else
}
} else {
return false; // year%4==0 else
}
} else {
return false; // this else associated with upper if
}
}
}
英文:
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; //year%400==0 else
}
}else {
return true; //year%100==0 else
}
}else {
return false; // year%4==0 else
}
}
else return false; //this else associated with upper if
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论