【发布时间】:2020-06-17 19:19:30
【问题描述】:
public class MonthDayCal {
public static boolean isLeapYear(int year) {
if (year < 1 || year > 9999)
return false;
else {
switch (year % 4) {
case 0:
if ((year % 100) == 0) {
if ((year % 400) == 0) {
return true;
} else
return false;
} else
return true;
break; // <--java : unreachable statement
default:
return false;
}
}
}
public static void main(String[] args) {
System.out.println(isLeapYear(2100));
}
}
在我的程序中使用 break 语句计算跳跃的问题 年份使用 switch 语句。为什么我不能在这里使用break? 如果我删除这个休息, 程序运行正确。当 year 可以被 4 整除并停止 switch 块时,我不应该使用 break 来摆脱情况吗?
【问题讨论】:
-
在
break;之前的所有情况下,您都会返回。这意味着它无法访问...cases 只能有return或break,因为它们都会导致离开范围。
标签: java switch-statement break