在我的程序中使用 break 语句使用 switch 语句计算闰年的问题
Problem with using break statement in my program to calculate leap year using switch statement
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));
}
}
Problem with using break statement in my program to calculate leap
year using switch statement. Why i can't use break here?
If i remove this break,
the program runs correct. Shouldn't i use break to get out of case when year is divisible by 4 and stop the switch block.
永远不会到达 break
语句,因为 case 0
的所有执行路径总是以 return 语句结束。
因此不需要。
如果您为变量赋值而不是 return 语句,则需要 break
语句:
boolean result = false;
if(year<1 || year>9999)
result = false;
else {
switch(year%4){
case 0:
if((year%100)==0){
if((year%400)==0){
result = true;
}
else
result = false;
}
else
result = true;
break;
default:
result = false;
}
}
return result;
当然,在那种情况下你可以简化代码,所以你根本不需要 break 语句:
boolean result = false;
if(year>=1 && year<=9999) {
switch(year%4){
case 0:
if((year%100)==0){
if((year%400)==0){
result = true;
}
} else {
result = true;
}
}
}
return result;
也就是说,这看起来不是使用 switch
语句的好场景,因为您只有两个案例(甚至一个)。
例如你可以这样写:
boolean result = false;
if(year>=1 && year<=9999 && year%4 == 0) {
if((year%100)==0){
if((year%400)==0){
result = true;
}
} else {
result = true;
}
}
return result;
甚至更简单(尽管还可以进一步简化):
boolean result = false;
if(year>=1 && year<=9999 && year%4 == 0) {
if((year%100) != 0 || (year%400)==0) {
result = true;
}
}
return result;
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));
}
}
Problem with using break statement in my program to calculate leap year using switch statement. Why i can't use break here? If i remove this break, the program runs correct. Shouldn't i use break to get out of case when year is divisible by 4 and stop the switch block.
永远不会到达 break
语句,因为 case 0
的所有执行路径总是以 return 语句结束。
因此不需要。
如果您为变量赋值而不是 return 语句,则需要 break
语句:
boolean result = false;
if(year<1 || year>9999)
result = false;
else {
switch(year%4){
case 0:
if((year%100)==0){
if((year%400)==0){
result = true;
}
else
result = false;
}
else
result = true;
break;
default:
result = false;
}
}
return result;
当然,在那种情况下你可以简化代码,所以你根本不需要 break 语句:
boolean result = false;
if(year>=1 && year<=9999) {
switch(year%4){
case 0:
if((year%100)==0){
if((year%400)==0){
result = true;
}
} else {
result = true;
}
}
}
return result;
也就是说,这看起来不是使用 switch
语句的好场景,因为您只有两个案例(甚至一个)。
例如你可以这样写:
boolean result = false;
if(year>=1 && year<=9999 && year%4 == 0) {
if((year%100)==0){
if((year%400)==0){
result = true;
}
} else {
result = true;
}
}
return result;
甚至更简单(尽管还可以进一步简化):
boolean result = false;
if(year>=1 && year<=9999 && year%4 == 0) {
if((year%100) != 0 || (year%400)==0) {
result = true;
}
}
return result;