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();
            }
        }
    }

  • 相关阅读:
    一种通用的简易缓存设计方案
    SpringCloud接入Passport中台服务的FeignClient简易集成配置
    一种基于P2P技术的高效数据传输方式
    应用多环境部署和Redis高可用
    瑞金小吃
    前(单页面)后端完全分离的OAuth2授权和分享
    Session(数据)共享的前后端分离Shiro实战
    10万Http(单机和集群Server)Subscribe的可行性实验和压测
    2018年你应该了解的前端新技术
    js常见问题总结归纳
  • 原文地址:https://www.cnblogs.com/lisu/p/2988974.html
Copyright © 2011-2022 走看看