for循环嵌套

来源:互联网 发布:最后一次网络歌手 编辑:程序博客网 时间:2024/06/03 16:40

​                                                        for循环嵌套

class DemoFor

{

    public static void main(String[] args){

            for(int  x=0;x<3;x++){

                for(int  y=0;y<4;y++){

                    System.out.println("ok");//结果12次

                }


        }


    }


}

注:大圈套小圈思想。



例1:

*****

*****

*****

*****

*****

class DemoFor1

{l

    public static void main(String[] args){

            for(int  x=0;x<4;x++){            //外循环控制的是行数

            for(int  y=0;y<5;y++){            //内循环控制的是次数

                System.out.print("*");        //这里的println的ln不要加。

                }

             System.out.println();

        }

    }

}

注:ln这个是换行的,不是所有的都能用


例2:

*****

****

***

**

*


class DemoFor3

{

    public static void main(String[] args){

        for(int x=1;x<=5;x++){

            for(int y=x;y<=5;y++){

                System.out.print("*");

                }

            System.ot.println();

        }

    }

}


例3:

54321

5432

543

54

5


class DemoFor4

{

    public static vodi main(String[] agrs){

        for(int x=1;x<=5;x++){

            for(int y=5;y>=x;y--){

                System.out.print(y);

                }

            System.out.println();

        }

    }

}


例4:

1

22

333

4444

55555


class DemoFor5

{

    public static void main(String[] args){

        for(int x=1;x>=5;x++){

            for(int y=1;y>=x;y++){

                System.out.print(y);

                }

            System.out.println();

        }

    }

}



例5:

九九乘法表

1*1=1

1*2=2  2*2=4

1*3=3  2*3=6  3*3=9

…………


class DemoFor6

{

    public static void main(String[] args){

        for(int x=1;x<=9;x++){

            for(int y=1;y>=x;y++){

                 System.out.print(y"*"+x+"="+y*x"\t");

                }                      //被乘//乘数 //两个的和                   

             System.out.printin();

        }

    }

}


注:


\t:制表符

\n:回车(“Hello\nWorld”)

\b:退格

\r:按下回车键

windows:系统中回车符其实是由两个符号组成的\r\n。

liunx:中回车符是\n。


(“\“HelloWorld\"")

(“\\Hello\nWorld\\”)






例6:

* * * * *

 * * * *

  * * *

   * *

    *


class DemoFor7{

    public static void main(String[] args){

            for(int x=1;x<=5;x++){

                for(int y=1;y<x;y++){

                    System.out.print(" ");   

                    }

             for(int z=x;z<=5;z++){     

                    System.ot.print("* ")//*后面加空格

                   }

        

         }

    }

}


                                            其他流程控制语句

 baeak;(跳出),continue(继续)

baeak:跳出

baeak作用的范围:要么是switch语句,要么是循环语句。

记住:当baeak语句单独存在时,下面不要定义其他语句,因为执行不到。

class DemoFor8{

    for(int x=0;x<3;x++){

        break;

        System.out.println("x="+x);

    }

}

注:如果continue单独存在时,下面不要有任何语句,因为执行不到


记住:break跳出所在的当前循环。如果出现了循环嵌套,

break想要跳出指定的循环,可以通过标号来完成。


例7:

class DemoFor8{

    public static void main(String[] args){

    xiaoqing  for(int x=0;x<3;x++){

          wangcai  for(int y=0;y<4;y++){

                                System.out.print("x="+x);

                                 break  xiaoqing;

                           }                //名字

               }

        }

}

注:起个名字就可以跳出指定的循环。


continue:继续

作用的范围:循环结构

continue:结束本次循环,继续下次循环

如果continue单独存在时,下面不要有任何语句,因为执行不到


例8:

class DemoFor9{

    public static void main(String[] args){

        for(int x=0;x<11;x++){

                continue;

                System.out.print("x="+x);

            }

    }

}

注:如果continue单独存在时,下面不要有任何语句,因为执行不到




0 0
原创粉丝点击