这样打代码哪里有错...求大神解答

来源:4-17 switch与if语句的应用

dodo66

2017-10-06 17:33

#include <stdio.h>

int main() 

    int year = 2008;

    int month = 8;

    int day = 8;

    int i,sum;

    int Jan = 31;

    int Feb = 28;

    int Mar = 31;

    int Apr = 30;

    int May = 31;

    int Jun = 30;

    int Jul = 31;

    int Aug = 31;

    int Sep = 30;

    int Oct = 31;

    int Nov = 30;

    int Dec = 31;

    if(year%100==0&&year%400==0||year%4==0&&year%100!=0)i=1;

    else i=0;

    switch(month)

    {

        case 1:sum = Jan break;

        case 2:sum = Jan+Feb break;

        case 3:sum = Jan+Feb+Mar break;

        case 4:sum = Jan+Feb+Mar+Apr break;

        case 5:sum = Jan+Feb+Mar+Apr+May break;

        case 6:sum = Jan+Feb+Mar+Apr+May+Jun break;

        case 7:sum = Jan+Feb+Mar+Apr+May+Jun+Jul break;

        case 8:sum = Jan+Feb+Mar+Apr+May+Jun+Jul+Aug break;

        case 9:sum = Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep break;

        case 10:sum = Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct break;

        case 11:sum = Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov break;

        case 12:sum = Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov+Dec break;

    }

    if(i==1)sum+=1;

    printf("%d年%d月%d日是该年的第%d天",year,month,day,sum);

    return 0;

}


写回答 关注

2回答

  • Aziraphale
    2017-10-06 19:27:21
    已采纳

    Jan的值应该赋为0,二月赋一月的天数,三月赋二月的天数,依次下去。比如一月3号,按照你的代码在计算时就会是第34天,实际上是第三天。不知道有没有讲清楚⊙﹏⊙

    dodo66

    有道理!我试试

    2017-10-10 17:26:31

    共 1 条回复 >

  • 慕少1559731
    2017-10-10 18:00:52

    可以把switch(month)改成switch(month-1)

C语言入门

C语言入门视频教程,带你进入编程世界的必修课-C语言

926212 学习 · 20797 问题

查看课程

相似问题