用Java写一个输入月份和天数输出这是多少天的代码
时间: 2023-05-20 18:04:23 浏览: 102
可以使用以下代码实现:
import java.util.Scanner;
public class DaysInMonth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入月份:");
int month = input.nextInt();
System.out.print("请输入天数:");
int days = input.nextInt();
int daysInMonth = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
if ((days % 4 == 0 && days % 100 != 0) || (days % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
System.out.println("输入的月份不正确!");
break;
}
System.out.println(month + "月" + days + "日是" + daysInMonth + "天。");
}
}
阅读全文