import java.util.Scanner;
public class SwithEx {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入年份");
int year =input.nextInt();
System.out.println("请输入月份");
int month=input.nextInt();
int numdays=switch(month){
case 1,3,5,7,8,10,12->31;
case 4,6,9,11->30;
case 2->{
if((year%4==0)&&!(year%100==0)||(year%400==0)){
yield 29;
}
else{
yield 28;
}
}
default->0;
};
System.out.println(numdays+"天");
}
}
java语言:从键盘输入一个年份(如2000)和一个月份(如2月),用Switch表达式返回该月的天数(如29),将其存入一个变量
于 2024-03-24 22:49:22 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)