通透易碎的天
2018-10-18 16:20
switch(month-1)
{
case 12:day+=31;
case 11:day+=30;
case 10:day+=31;
case 9:day+=30;
case 8:day+=31;
case 7:day+=31;
case 6:day+=30;
case 5:day+=31;
case 4:day+=30;
case 3:day+=31;
case 2:if((year%4==0 && year%100) || year%400==0) day+=29; else day+=28;
case 1:day+=31;
}
printf("2008年8月8日是该年的第%d天",day);
为什么是month-1
你这个语句有问题,因为当month=1时,会无限循环。
应该
#include <stdio.h>
int main()
{
int year = 2008;
int month = 8;
int day = 8;
int i=0;
if(year%4==0)
{
switch(month)
{
case 12:i+=30;
case 11:i+=31;
case 10:i+=30;
case 9:i+=31;
case 8:i+=31;
case 7:i+=30;
case 6:i+=31;
case 5:i+=30;
case 4:i+=31;
case 3:i+=29;
case 2:i+=31;
case 1:i+=day;break;
default:break;
}
printf("%d年%d月%d日是该年的第%d天",year,month,day,i);
}
else
{
switch(month)
{
case 12:i+=30;
case 11:i+=31;
case 10:i+=30;
case 9:i+=31;
case 8:i+=31;
case 7:i+=30;
case 6:i+=31;
case 5:i+=30;
case 4:i+=31;
case 3:i+=28;
case 2:i+=31;
case 1:i+=day;break;
default:break;
}
printf("%d年%d月%d日是该年的第%d天",year,month,day,i) ;
}
return 0;
}
C语言入门
926020 学习 · 20793 问题
相似问题