这个代码为什么会算出累加的和呀,而且加不加break在这个代码中会有怎样的影响。

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

慕九州7466166

2017-08-17 23:02

#include <stdio.h>

int main() 

    /* 定义需要计算的日期 */

    int year = 2008;

    int month = 8;

    int day = 8;

    /*

     * 请使用swtich语句,if...else语句完成本题

     * 如有想看小编思路的,可以点击左侧任务中的“不会了怎么办”

     * 小编还是希望大家独立完成哦~

     */

    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);

return 0;

}


写回答 关注

2回答

  • 码农预备役
    2017-08-18 10:59:02
    已采纳

    因为没有添加break所以会依次执行

    例如月份是5的时候

    会执行

           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;

    这四句代码

    慕九州746...

    非常感谢!

    2017-08-18 13:11:45

    共 1 条回复 >

  • 我是弱弱呀
    2017-08-18 11:20:41

    在case子句后如果没有break;会一直往后执行一直到遇到break;才会跳出switch语句。

C语言入门

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

927005 学习 · 21533 问题

查看课程

相似问题