JAVA 基础知识点复习(四)三大控制结构


这系列博文主要是将自己之前学习的一些java基础内容进行归纳与总结。

顺序结构

从上至下,从左至右

选择结构

  • (1) if…else
if (true) {}

if (true) {} else {}

if (true) {} else if (true) {} else {}
  • (2) switch
switch (1) {
    case 1:
        System.out.println("A");
        break;
    case 2:
        System.out.println("B");
        break;
    default:
        System.out.println("C");
}

switch 后可以跟char、byte、short、int、Character、Byte、Short、Integer、String(jdk1.7后支持)、enum

如果没有遇到break或return,则从第一个case匹配到后一直往下执行

如果switch后是枚举类,则case后面必须要是枚举枚举常量的非限定名称

switch (weekEnum){
    case WeekEnum.MONDAY: // 编译异常 An enum switch case label must be the unqualified name of an enumeration constant
    case TUESDAY: // 正确写法

如果switch后是null,则会报空指针异常

String str = null;
switch (str){ // 'NullPointerException'
}

循环结构

  • (1) while
while (true) {}
do {} while (true); // 最少执行一次方法体
  • (2) for
for (int i=0; i<10; i++) {
    // do something
}

while循环控制的变量作用于整个方法,而for循环控制的变量只作用于for循环

循环嵌套

    public static void main(String[] args) {
        int row = 4;
        t(row);
        t1(row);
        t2(row);
        t3(row);
        t4(row);
        t5(row);
    }

    /**
     * 列随着行的增多而增多
     * *
     * **
     * ***
     * ****
     */
    private static void t(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 列随着行的增多而减少
     * ****
     * ***
     * **
     * *
     */
    private static void t1(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row -i; j++) {
                System.out.print("*");
            }
            System.out.println();

        }
    }

    /**
     * 列随着行的增多而增多,并且需要控制空格站位
     *    *
     *   **
     *  ***
     * ****
     */
    private static void t2(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row-i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 列随着行的增多而减少,并且需要控制空格站位
     * ****
     *  ***
     *   **
     *    *
     */
    private static void t3(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < row -i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 范围从小到大,掌握中值
     *    *
     *   ***
     *  *****
     * *******
     */
    private static void t4(int row){
        int column = 2 * row -1;
        int median = column / 2;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j >= median - i && j <= median + i) {
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    /**
     * 范围从大到小
     * *******
     *  *****
     *   ***
     *    *
     */
    private static void t5(int row){
        int column = 2 * row -1;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j >= i && j< column - i) {
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

break和continue

break:终止循环,只作用于switch和循环语句(for/while),如果存在循环嵌套则只作用于当前循环。

continue:跳出当前循环,进入下次循环,只作用于循环语句,如果存在循环嵌套则只作用于当前循环。

break和continue都可以通过标识符来控制所需结束的循环

outer:
for (int i = 0; i < 3; i++) {
    inner:
    for (int j = 0; j < 3; j++) {

        for (int k = 0; k < 3; k++) {
            break outer; // 直接结束最外层循环
        }
    }
}

无限循环的两种写法

for (; ; ) {}

while (true) {}

趣味题

打印所有水仙花数(一个 3 位数,它的每个位上的数字的3次幂之和等于它本身)

    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            int n = (i / 100);
            int m = (i / 10 % 10);
            int k = (i % 10);
            int multiply = (int) (Math.pow(n, 3) + Math.pow(m, 3) + Math.pow(k, 3));
            if (multiply == i) {
                System.out.println(i);
            }
        }
    }
原创文章 7 获赞 0 访问量 6219

猜你喜欢

转载自blog.csdn.net/weixin_42598683/article/details/105280736