用Java编写程序。提示用户输人一个年份和一个月份名称的前三个字母(第一 个字母使用大写形式),显示该月中的天数。下面是一个运行示例: 示例1; 输入:2001 Jan 输出:Jan 2001 has 31 days
时间: 2024-05-09 09:17:17 浏览: 106
好的,您的问题很简单。以下是用Java编写的程序,它会提示用户输入年份和月份名称的前三个字母,并返回该月中的天数。
```
import java.util.Scanner;
public class DaysInMonth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for year and month
System.out.print("Enter the year: ");
int year = input.nextInt();
System.out.print("\nEnter a month (first three letters): ");
String month = input.next();
int daysInMonth = 0;
// Determine the number of days in the month
switch (month.toUpperCase()) {
case "JAN":
case "MAR":
case "MAY":
case "JUL":
case "AUG":
case "OCT":
case "DEC":
daysInMonth = 31;
break;
case "APR":
case "JUN":
case "SEP":
case "NOV":
daysInMonth = 30;
break;
case "FEB":
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
daysInMonth = 29;
}
else {
daysInMonth = 28;
}
break;
default:
System.out.println("Invalid month entered.");
System.exit(0);
}
// Display the number of days in the month
System.out.println(month.substring(0,1).toUpperCase() + month.substring(1,3) + " " + year + " has " + daysInMonth + " days.");
}
}
```
您可以将此代码复制并粘贴到您的Java IDE中,然后运行该程序,即可通过命令行输出您想要的结果。
阅读全文