您好,请问S1E17~18这题怎么写?
#include<stdio.h>int main()
{
int year = 0;
int month = 1;
int day =0;
int flag = 0;
printf("请输入一个年份:");
scanf("%d",year);
if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
{
flag = 1;
}
//判断是否为闰年
for(month = 1;month < 13;month ++)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
printf("%d月份:%d天",month,day);
continue;
case 4:
case 6:
case 9:
case 11:
day = 30;
printf("%d月份:%d天",month,day);
continue;
case 2:
if(flag)
{
day = 29;
printf("%d月份:%d天",month,day);
continue;
}
else
{
day = 28;
printf("%d月份:%d天",month,day);
continue;
}
}
}
return 0;
}
以上是我写的代码,请问哪里出现了问题? #include<stdio.h>
int main()
{
int year = 0;
int month = 1;
int day =0;
int flag = 0;
printf("请输入一个年份:");
scanf("%d",&year); // 少了&
if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
{
flag = 1;
}
//判断是否为闰年
for(month = 1;month < 13;month ++)
{
//下面打印的时候少了换行,而且月份需要右对齐
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
printf("%2d月份:%d天\n",month,day);
continue;
case 4:
case 6:
case 9:
case 11:
day = 30;
printf("%2d月份:%d天\n",month,day);
continue;
case 2:
if(flag)
{
day = 29;
printf("%2d月份:%d天\n",month,day);
continue;
}
else
{
day = 28;
printf("%2d月份:%d天\n",month,day);
continue;
}
}
}
return 0;
} 本帖最后由 jackz007 于 2022-11-10 10:36 编辑
#include<stdio.h>
int main()
{
int year = 0;
int month = 1 ;
int day = 0 ;
int flag = 0;
printf("请输入一个年份:") ;
scanf("%d", & year) ; // 【致命错误,修改】
if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
{
flag = 1;
}
//判断是否为闰年
for(month = 1;month < 13;month ++)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
printf("%2d月份:%d天\n",month,day); // 【修改】
continue;
case 4:
case 6:
case 9:
case 11:
day = 30;
printf("%2d月份:%d天\n",month,day); // 【修改】
continue;
case 2:
if(flag)
{
day = 29;
printf("%2d月份:%d天\n",month,day); // 【修改】
continue;
}
else
{
day = 28;
printf("%2d月份:%d天\n",month,day); // 【修改】
continue;
}
}
}
return 0;
}
下面是我写的代码,谨供楼主参考
#include <stdio.h>
int main(void)
{
int a[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} , d , m , y ;
printf("请输入一个年份:") ;
scanf("%d" , & y) ;
for(m = 0 ; m < 12 ; m ++) {
d = a ;
if(m == 1 && (! (y % 4) && y % 100) || ! (y % 400)) d ++ ;
printf("%2d月份:%d天\n" , m + 1 , d) ;
}
} jackz007 发表于 2022-11-10 10:29
下面是我写的代码,谨供楼主参考
膜拜大佬
页:
[1]