java使用switch求某年某月天数
展开全部
看代码:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws Exception {
try (Scanner in = new Scanner(System.in)) {
System.out.println("输入年份和月份:");
int year = in.nextInt();
int month = in.nextInt();
int day = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day = 29;
} else {
day = 28;
}
break;
default:
System.err.println("输入月份有误");
break;
}
System.out.printf("%d 年 %d 月有 %d 天\n", year, month, day);
}
}
}
运行:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询