使用switch语句实现:计算并输出某年某月天数

分析程序

对12个月进行分析,合并出来三种情况。

情况1:31天的月份,1、3、5、7、8、10、12月;

情况2:30天的月份:4、6、9、11月;

情况3:2月通过年份使用if-else判断是闰年当月29天,平年28天。


程序实现

#include<stdio.h>
int main()
{
	int year,month,days;
	printf("输入年月:");
	scanf("%d%d",&year,&month);
	switch(month)
	{
		case 1:
		case 3: 
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:days=31;break;
		case 4:		
		case 6:		
		case 9:		
		case 11:days=30;break;
		case 2:
			if(year%4==0&&year%100!=0||year%400==0)
				days=29;
			else
				days=28;break;				
		default:printf("非法数字");
	}
	printf("%d年%d月有%d天",year,month,days);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/studyup05/article/details/130196788