Java基础之嵌套循环

嵌套循环的使用
1.嵌套循环:将一个循环结构A声明在另一个循环结构b的循环体中,就构成了循环嵌套
2.外层循环:循环体b;
内层循环:循环体a;
打印一个正方形
3.外层控制行数;内行控制列
 for (int y = 1; y<=9;y++){
            for (int L = 1; L <=9;L++){
                System.out.print("*"+"  ");
            }
            System.out.println();
        }
测试
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  

  打印直角三角形

for (int t=1;t<10;t++) {
            for (int j = 1; j <10; j++) {
                if (j<t||j==t) {
                    System.out.print("* " );
                }
            }
            System.out.println();
        }
测试
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 

  打印乘法口诀表

for (int t = 1; t < 10; t++) {
            for (int j = 1; j < 10; j++) {
                if (j < t || j == t) {
                    int g = t * j;
                    System.out.print(t + "*" + j + "=" + g + " ");
                }
            }
            System.out.println();
 }
测试
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

  打印三角形 ,打印乘法口诀表,第二种方法;

//打印直角三角形
        for (int t = 1; t < 10; t++) {
            for (int j = 1; j <= t; j++) {
                //if (j < t || j == t) {
                    System.out.print("* ");
                //}
            }
            System.out.println();
        }
        System.out.println("
");
        //乘法口诀表;
        for (int t = 1; t <10; t++) {
            for (int j = 1; j <= t; j++) {
               // if (j < t || j == t) {
                    int g = t * j;
                    System.out.print(t + "*" + j + "=" + g + " ");
               // }
            }
            System.out.println();
        }
测试
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 


1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

  打印倒立三角形

        System.out.println("
");
        for (int t=1;t<10;t++){
            for (int j = 1;j<= 10-t;j++){
                System.out.print("* ");
            }
            System.out.println();
        }

测试
* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

  打印100以内的质数

for (int t = 2 ; t<100; t++ ){
            int sun = 0;
            for (int y =2 ; y<t;y++){
                if (t%y == 0) {
                    sun ++;
                }
            }
            if (sun==0){
                System.out.println(t);
            }
        }

测试
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

  优化,也要根据实际情况数值如果太小不建议使用开方,它反而会慢

long start = System.currentTimeMillis();
        for (int t = 2 ; t< 100000; t++ ){
            int sun = 0;
            for (int y =2 ; y< t;y++){
            //for (int y =2 ; y<=Math.sqrt(t);y++){// 使用开方
                if (t%y == 0) {
                    sun ++;
                    //break;//优化
                }
            }
            if (sun==0){
                System.out.println(t);
            }
        }
        long stop = System.currentTimeMillis();
        long s = stop - start;
        System.out.println("所花费的时间:"+s);
        //不优化时间21609;1打开break,1583;1使用开方121;2

  关键字break与continue的使用;关键字的后面不能使用语句

 //关键字break与continue的使
        for (int t= 1; t<10 ;t++){
            if (t%4==0){
                //break;//终止当前循环体,结果123
                continue;//打断本次循环,继续下一次循环,结果1235679
            }
            System.out.print(t);
        }


测试
1235679

  示例2

       k:for(int i = 1;i<=4;i++){
            for (int j=1;j<=10;j++){
                if (j%4==0){
                    //break k;//终止外层循环体;结果123
                    continue k;//打断外层循环体的本次循环;结果123123123123
                }
                System.out.print(j);
            }
        }
测试
123123123123


       k:for(int i = 1;i<=4;i++){
            for (int j=1;j<=10;j++){
                if (j%4==0){
                    break k;//终止外层循环体;结果123
                    //continue k;//打断外层循环体的本次循环;结果123123123123
                }
                System.out.print(j);
            }
        }
测试
123

  

原文地址:https://www.cnblogs.com/rdchenxi/p/13377898.html