不知道哪儿有错

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

慕尼黑2539677

2017-12-12 21:10

#include <stdio.h>
int main()
{
    int n;
    int year = 2008;
    int month = 8;
    int day = 8;
    if( year%4 == 0)
    {
        switch(month)
        {
            case 1:n=day;
                    break;
            case 2:n=31+day;
                    break;
            case 3:n=31+28+day;
                    break;
            case 4:n=31+28+31+day;
                    break;
            case 5:n=31+28+31+30+day;
                    break;
            case 6:n=31+28+31+30+31+day;
                    break;
            case 7:n=31+28+31+30+31+30+day;
                    break;
            case 8:n=31+28+31+30+31+30+day+31;
                    break;
            case 9:n=31+28+31+30+31+30+day+31+31;
                    break;
            case 10:n=31+28+31+30+31+30+day+31+31+30;
                    break;
            case 11:n=31+28+31+30+31+30+day+31+31+30+31;
                    break;
            case 12:n=31+28+31+30+31+30+day+31+31+30+31+30;
                    break;
        }
    }
    else
    {
        switch(month)
        {
            case 1:n=day;
                    break;
            case 2:n=31+day;
                    break;
            case 3:n=31+29+day;
                    break;
            case 4:n=31+29+31+day;
                    break;
            case 5:n=31+29+31+30+day;
                    break;
            case 6:n=31+29+31+30+31+day;
                    break;
            case 7:n=31+29+31+30+31+30+day;
                    break;
            case 8:n=31+29+31+30+31+30+day+31;
                    break;
            case 9:n=31+29+31+30+31+30+day+31+31;
                    break;
            case 10:n=31+29+31+30+31+30+day+31+31+30;
                    break;
            case 11:n=31+29+31+30+31+30+day+31+31+30+31;
                    break;
            case 12:n=31+29+31+30+31+30+day+31+31+30+31+30;
                    break;
        }
    }
    printf("%d",n);
    return 0;
}
写回答 关注

1回答

  • 墨夜如歌
    2017-12-12 21:50:46

    闰年的判断条件不对,应该是

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

    你再试试


C语言入门

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

926211 学习 · 20797 问题

查看课程

相似问题