Java基础之流程控制

public class ForStu {
    public static void main(String[] args) {
        System.out.println("java 的 流程控制");
        int a = 2;
        int b = testIf(a);
        System.out.println("b==" + b);
        testSwitch(2);
        int num = 1;
        testFor(num);
        testWhile(a);
        testDoWhile(2);
        testSum();
    }

    public static int testIf(int a) {
        if (a == 0) {
            a += 1;
        } else if (a == 1) {
            a += 2;
        } else if (a == 2) {
            a += 3;
        } else {
            a += 4;
        }
        return a;
    }

    public static void testSwitch(int aa) {
        // python 中没有这东西
        /*
         * switch 的入参 只能是基本数据类型 byte short char int 引用数据类型 String enum
         * 多个case后的树枝不可以重复
         * break可省略 且case语句可以无序
         * */
        switch (aa) {
            case 1:
                System.out.println("AAA");
                break;

            case 2:
                System.out.println("BBB");
                break;

            default:
                System.out.println("CCC");
                break;
        }
    }

    public static void testFor(int a) {
        /*
         * for(初始化表达式1;布尔表达式2;步进表达式4){
         *              循环体3
         * }
         *  1234--234---234
         * */
        for (int i = a; i < 10; i++) {
            System.out.println("循环====" + i);
        }
    }

    public static void testWhile(int b) {
        while (b < 10) {
            System.out.println("while b=" + b);
            b++;
        }
    }

    public static void testDoWhile(int c) {
        // do while 循环 先执行do循环体 然后在走while
        do {
            System.out.println("do while c=" + c);
            c++;
        } while (c < 5);
    }

    public static int testSum() {
        int a = 0;
        int b = 0;
        while (a <= 100) {
            if (a % 2 == 0) {
                b = a + b;
            }
            a++;
        }
        System.out.println("sumtest b=" + b);
        return b;
    }
}

  

posted @ 2021-06-25 14:13  Yuan_x  阅读(22)  评论(0编辑  收藏  举报