黑马程序员技术交流社区

标题: 关于switch循环 [打印本页]

作者: Alonesss    时间: 2017-3-4 09:46
标题: 关于switch循环
public static int getValue(int i) {
        int result = 0;
        switch (i) {
        case 1:
            result = result + i;
        case 2:
            result = result + i * 2;
        case 3:
            result = result + i * 3;
        }
        return result;
    }
当输入为2的时候返回值为什么是10

作者: 北海北    时间: 2017-3-4 17:58
因为你的每个case后面没有break,刚开始先执行case2,执行完,接着执行case3





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2