zoukankan      html  css  js  c++  java
  • 20130329java基础学习笔记语句_for循环嵌套

    1.for循环嵌套讲解:
    class ForForDemo
    {
        public static void main(String[] args)
        {
            //大圈套小圈思想:
            /*
            for(int x=0; x<2; x++)
            {
                for(int y=0; y<2; y++)
                {
                    System.out.println("*****");
                }//内循环
            }//外循环
            */
            /*
            练习:需求:在控制台输出以下图:
            *****
            *****
            *****
            *****
            */
            /*只用一个for循环实现:
            for(int x=0; x<4; x++)
            {
                System.out.println("*****");
            }
            */
            for(int x=1; x<5; x++)//外循环控制的是行数;
            {
                for(int y=1; y<6; y++)//内循环控制的是每一行的列(个)数;
                {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

    2.for循环嵌套_练习1:
    class ForForTest
    {
        public static void main(String[] args)
        {
            /*
            需求1:用循环做出以下图形
            *****
            ****
            ***
            **
            *
            方法一:
            int z=5;
            for(int x=1; x<=5; x++)//1-5 1-4 1-3 1-2
            {
                for(int y=1; y<=z; y++)
                {
                    System.out.print("*");
                }
                System.out.println();
                z--;
            }
            */
            /*
            方法二:
            int z=1;
            for(int x=1; x<=5; x++)//1-5 2-5 3-5 4-5
            {
                for(int y=z; y<=5; y++)
                {
                    System.out.print("*");
                }
                z++;
                System.out.println();
            }
            /*
            /*
            方法三:
            for(int x=1; x<=5; x++)
            {
                for(int y=x; y<=5; y++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
            */
            /*
            方法四:
            for(int x=5; x>0; x--)
            {
                for(int y=1; y<=x; y++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
            */
            /*

         方法五:
         for(int x=0;x<5;x++)

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

           {
              System.out.print("*");
            }
            System.out.println();
         }

          */


            /*
            需求2:用循环制作出以下图:
            *
            **
            ***
            ****
            *****
            方法一:
            for(int a=1; a<=5; a++)
            {
                for(int b=1; b<=a; b++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
            */
            /*
            方法二:*/
            for(int x=5; x>0; x--)
            {
                for(int y=5; y>=x; y--)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
            
        }
    }

    3.for循环嵌套_练习2:
    class ForForTest
    {
        public static void main(String[] args)
        {
            /*
            54321
            5432
            543
            54
            5
            */
            for(int x=1; x<=5; x++)
            {
                for(int y=5; y>=x; y--)
                {
                    System.out.print(y);
                }
                System.out.println();
            }
            /*
            1
            22
            333
            4444
            55555
            */
            for(int x=1; x<=5; x++)
            {
                for(int y=1; y<=x; y++)
                {
                    System.out.print(x);
                }
                System.out.println();
            }
        }
    }

  • 相关阅读:
    【cdq分治】【P4390】[BOI2007]Mokia 摩基亚
    【树上莫队】【SP10707】 COT2
    【费用流】【网络流24题】【P1251】 餐巾计划问题
    【费用流】【网络流24题】【P4014】 分配问题
    【MST】P2323 [HNOI2006]公路修建问题
    【组合数学】【P5216】DLS采花
    【线段树】【P4198】 楼房重建
    【整体二分】【P3527】 [POI2011]MET-Meteors
    【线性基/神仙题】P4151 [WC2011]最大XOR和路径
    【枚举&数据结构】【P2484】 [SDOI2011]打地鼠
  • 原文地址:https://www.cnblogs.com/lisu/p/2988974.html
Copyright © 2011-2022 走看看