用Java的switch语句写一个输入2023年的月“month”和日“day”两个参数,计算出该日期为2023年的第几天,并打印输出“month月day日是2023年第x天”。
时间: 2023-05-24 11:02:57 浏览: 198
输入年和月的信息,输出该月对应的天数。注意合理性与闰年的判别。(switch语句练习)
5星 · 资源好评率100%
A:
```java
import java.util.Scanner;
public class DayOfYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
int day = sc.nextInt();
sc.close();
int dayOfYear = 0;
switch (month) {
case 1:
dayOfYear = day;
break;
case 2:
dayOfYear = 31 + day;
break;
case 3:
dayOfYear = 59 + day;
break;
case 4:
dayOfYear = 90 + day;
break;
case 5:
dayOfYear = 120 + day;
break;
case 6:
dayOfYear = 151 + day;
break;
case 7:
dayOfYear = 181 + day;
break;
case 8:
dayOfYear = 212 + day;
break;
case 9:
dayOfYear = 243 + day;
break;
case 10:
dayOfYear = 273 + day;
break;
case 11:
dayOfYear = 304 + day;
break;
case 12:
dayOfYear = 334 + day;
break;
default:
System.out.println("输入无效的月份!");
return;
}
System.out.println(month + "月" + day + "日是2023年第" + dayOfYear + "天");
}
}
```
阅读全文