月份 天数 作业

38 阅读1分钟

运用Switch语句,输入月份,试试输出的天数为几天

#include <stdio.h>
int main(){
	// 输入月份,输出当月的天数
	// 1--------->31
	// 2----------> 继续询问用户,请输入年份,根据是否闰年判断,输出28,29
	// 3--------->31
	// 4--------->30
	// 5--------->31
	// 6--------->30
	// 7--------->31
	// 8--------->31
	// 9--------->30
	// 10-------->31
	// 11-------->30
	// 12-------->31 
	int month,year;
	printf("输入1-12的数字,获取月份天数:");
	scanf("%d", &month);
	
	switch(month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			printf("有31天");
			break;
		case 2:
		    printf("请输入年份");
			  scanf("%d", &year); 
			  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
			  	printf("有29天");
		    } else {
		    	printf("有28天"); 
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			printf("有30天");
			break;
		default:
		     printf("输入错误,没有这个月"); 
		
	}
}

image.png

image.png