递归和循环

1.递归代码

 执行结果

 启示:不要轻易使用递归,尤其是在方法内有对数据库的新增操作时

 

2.循环

// 使用continue
    private static void method2(){
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("break前"+i + " - " + j);
                if (j >= 1) {
                    continue;
                }
                System.out.println("break后"+i + " - " + j);
            }
            System.out.println("i的值" +i );
        }
    }

  

// 使用break
private static void method2(){
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("break前"+i + " - " + j);
                if (j >= 1) {
                    break;
                }
                System.out.println("break后"+i + " - " + j);
            }
            System.out.println("i的值" +i );
        }
    }

  

// 使用continue retry 
  private static void method2(){
        retry:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("break前"+i + " - " + j);
                if (j >= 1) {
                    continue retry;
                }
                System.out.println("break后"+i + " - " + j);
            }
            System.out.println("i的值" +i );
        }
    }

 

// 使用break retry

    private static void method2(){
        retry:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("break前"+i + " - " + j);
                if (j >= 1) {
                    break retry;
                }
                System.out.println("break后"+i + " - " + j);
            }
            System.out.println("i的值" +i );
        }
    }

 

posted on 2022-06-12 00:57  leaverk  阅读(59)  评论(0编辑  收藏  举报