用switch写月份天数

62 阅读1分钟

用switch写月份案例,内容关于输入月份,输出这个月有几天

1.代码:

#include<stdio.h>
int main(){
	int mouth,year,days;

	printf("请输入月份:");
	scanf("%d",&mouth); 
		
switch(mouth){
	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:
	  printf("请输入年份:");
	  scanf("%d",&year);
	  if((year%4==0&&year%100!=0)||(year%400!=0)){
	  	days=29;
	  }else{
	  	days=28;
	  }
      break;
    default:
    	printf("输入有误,月份应该在1-12之间。\n");
    	break;
      }
      printf("%d月有%d天\n",mouth,days);
}

运行结果

1.31天的运行结果

image.png

2.30天的运行结果

image.png

  1. 2月平年

image.png

4.2月润年

image.png