您的位置:首页 > 其它

switch语句练习——计算这一天是这一年的第几天

2014-09-16 20:22 447 查看
<pre name="code" class="objc"><span style="color:#ff6666;">#include <stdio.h>

int main(int argc, const char * argv[])
{
//用switch语句计算出这一天是这一年的第几天。

int year;
int mouth;
int day;
int isRun = 1;
int total;
printf("请输入年,月,日:\n");
scanf("%i,%i,%i",&year,&mouth,&day);

//如果是闰年的话,isRun是0
if(year % 100 == 0 && year % 4 == 0){
isRun = 1;
}else isRun = 0;

total = 0;

switch (mouth - 1)
{
case 12:
total += 31;
case 11:
total += 30;
case 10:
total += 31;
case 9:
total += 30;
case 8:
total += 31;
case 7:
total += 31;
case 6:
total += 30;
case 5:
total += 31;
case 4:
total += 30;
case 3:
total += 31;
case 2:
total += 28 + isRun;//如果是闰年,isRun的值是1,否则是0;
case 1:
total += 31;
total = total + day;
break;
}
printf("%d年,%d月,%d日是这一年的第%i天\n",year,mouth,day,total);

return 0;
}
</span>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐