利用switch语句输出月份天数

26 阅读1分钟

要求

输入月份,输出月份天数,若输入2则继续询问用户,输入年份,根据是否闰年的判断,输出28或29

代码实例

int main() {
	int year;
	int month;
	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 4 :
		case 6 :
		case 9 :
		case 11 :
			printf("本月有30天");
			break;
		default:
			printf("请输入年份:");
			scanf("%d",&year);
			if ((year % 4 == 0 && year % 100 !=0)||(year % 400 ==0)){
                            printf("今年是闰年,本月有29天。"); 
		
                        }else{
                            printf("今年是平年,本月有28天。"); 
                        }
			break;
	}
	
	return 0;
}

运行结果1:

image.png

运行结果2:

image.png

运行结果3:

image.png