帮我写一份输入年月日,输出该年有多少天,该月有多少天的c++代码用switch语法
时间: 2023-03-25 15:02:26 浏览: 108
用switch语句判断每个月有多少天
好的,以下是代码:
```c
#include <stdio.h>
int main() {
int year, month, day, days_in_month;
printf("请输入年份: ");
scanf("%d", &year);
printf("请输入月份: ");
scanf("%d", &month);
switch (month) {
case 1:
days_in_month = 31;
break;
case 2:
if ((year % 4 == && year % 100 != ) || year % 400 == ) {
days_in_month = 29;
} else {
days_in_month = 28;
}
break;
case 3:
days_in_month = 31;
break;
case 4:
days_in_month = 30;
break;
case 5:
days_in_month = 31;
break;
case 6:
days_in_month = 30;
break;
case 7:
days_in_month = 31;
break;
case 8:
days_in_month = 31;
break;
case 9:
days_in_month = 30;
break;
case 10:
days_in_month = 31;
break;
case 11:
days_in_month = 30;
break;
case 12:
days_in_month = 31;
break;
default:
printf("输入的月份无效,请输入1-12之间的数字。\n");
return 1;
}
printf("%d年%d月有%d天。\n", year, month, days_in_month);
return ;
}
```
阅读全文