...月的天数,比如输入是1,则输出31天,要求使用if语句
发布网友
发布时间:2024-10-08 19:23
我来回答
共1个回答
热心网友
时间:2024-10-08 19:58
注意一下二月就行了,别的月份都是死的
Scanner input = new Scanner(System.in);
int number_Of_DaysInMonth = 0;
String MonthOfName = "Unknown";
System.out.print("Input a month number: ");
int month = input.nextInt();
System.out.print("Input a year: ");
int year = input.nextInt();
if (month == 1){
MonthOfName = "January";
number_Of_DaysInMonth = 31;
}else if(month == 2){
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
}else if (month == 3){
MonthOfName = "March";
number_Of_DaysInMonth = 31;
}