11. Use Switch to output the maximum number of days in a month

/*
Enter the month score on the keyboard, and use the Switch to output the maximum number of days in the month.

Requirements:
1. The data must be received correctly
2. The number of months must be between 1-12 to

understand the Switch:
1. If the results of multiple Cases are the same, then
all the previous results and break can be omitted, leaving only the last result And break
2. The result of the expression enclosed in switch parentheses can only be an integer or a single character
. 3. If the results of multiple Cases are the same, you can also use default
to summarize them into one sentence.
4. The case value after case cannot be repeated
*/
#include "stdio.h"
void main()
{
int y,m,days;
int n;

n=0;
printf("Please enter the number of years:") ;
yy:n=scanf("%d",&y);
if(n!=1||y<1900)
{
printf("The year is wrong, please retype:");
fflush(stdin);
goto yy ;
}

n=0;
printf("Please enter the number of months:");
mm:n=scanf("%d",&m);
if(n!=1||m<1||m>


fflush(stdin);
goto mm;
}

switch(y)
{
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(y%4==0&&y%100!=0||y%400==0)
days=29;
else
days=28;
break;
}
printf("%d年%d月的最大天数是:%d天\n",y,m,days);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813001&siteId=291194637