帮孩子看看题吧
#include <stdio.h>int main()
{
int year,month;
scanf("%d %d",&year,&month);
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("leap\n");
else
printf("not leap\n");
switch(month)
{
case 1:printf("winter\ndays=31\n");break;
case 2:printf("winter\ndays=29\n");break;
case 3:printf("spring\ndays=31\n");break;
case 4:printf("spring\ndays=30\n");break;
case 5:printf("spring\ndays=31\n");break;
case 6:printf("summer\ndays=30\n");break;
case 7:printf("summer\ndays=31\n");break;
case 8:printf("summer\ndays=31\n");break;
case 9:printf("autumn\ndays=30\n");break;
case 10:printf("autumn\ndays=31\n");break;
case 11:printf("autumn\ndays=30\n");break;
case 12:printf("winter\ndays=31\n");break;
default:printf("error data");break;
}
return 0;
}
上面的代码自己敲的
他这个运行的时候 如果二月是闰月是29天 二月不是闰月是28天 不知道怎么变二月的 如果输入的格式为1992 13(错误的情况) 不知道怎么只输出一个 error data 不把leap或者not leap显示出来 . 二月的会了在里面加if
如果输入的格式为1992 13(错误的情况) 不知道怎么只输出一个 error data 不把leap或者not leap显示出来 这个咋弄、
在输入后面加个判断就好了。
scanf("%d %d",&year,&month);
//提前判断输入是否正确
if(year<=0 || month<=0 || month>12)
{
printf("error data");
return 0;
}
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("leap\n");
else
printf("not leap\n");
比如输入1992 13
答案要的格式:
error data
我的:
not leap
error data #include <stdio.h>
int main()
{
int n,year,month;
scanf("%d %d",&year,&month);
if((year%4==0)&&(year%100!=0)||(year%400==0))
{
n = 29;
printf("leap\n");
}
else
{
n = 28;
printf("not leap\n");
}
if(month == 2 && n == 28)
month = 13;
switch(month)
{
case 1:printf("winter\ndays=31\n");break;
case 2:printf("winter\ndays=29\n");break;
case 3:printf("spring\ndays=31\n");break;
case 4:printf("spring\ndays=30\n");break;
case 5:printf("spring\ndays=31\n");break;
case 6:printf("summer\ndays=30\n");break;
case 7:printf("summer\ndays=31\n");break;
case 8:printf("summer\ndays=31\n");break;
case 9:printf("autumn\ndays=30\n");break;
case 10:printf("autumn\ndays=31\n");break;
case 11:printf("autumn\ndays=30\n");break;
case 12:printf("winter\ndays=31\n");break;
case 13:printf("winter\ndays=28\n");break;
default:printf("error data");break;
}
return 0;
} #include <stdio.h>
int main()
{
int year,month;
scanf("%d %d",&year,&month);
if(month<1||month>12)
{
printf("error data");
exit(0);
}
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("leap\n");
else
printf("not leap\n");
switch(month)
{
case 1:printf("winter\ndays=31\n");break;
case 2:
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("winter\ndays=29\n");
else
printf("winter\ndays=28\n");break;
case 3:printf("spring\ndays=31\n");break;
case 4:printf("spring\ndays=30\n");break;
case 5:printf("spring\ndays=31\n");break;
case 6:printf("summer\ndays=30\n");break;
case 7:printf("summer\ndays=31\n");break;
case 8:printf("summer\ndays=31\n");break;
case 9:printf("autumn\ndays=30\n");break;
case 10:printf("autumn\ndays=31\n");break;
case 11:printf("autumn\ndays=30\n");break;
case 12:printf("winter\ndays=31\n");break;
default:printf("error data");break;
}
return 0;
}
页:
[1]