zoukankan      html  css  js  c++  java
  • for 的多重循环--java

                                                                                        for的多重循环--java

           利用for的多重循环打印出四种不同的三角形的图案。

      

         图案如下:

    4种不同三角形图案打印如下
    ------------------
    *
    **
    ***
    ------------------
    ***
    **
    *
    ------------------
      *
     **
    ***
    ------------------
    ***
     **
      *
    ------------------

    code :

    import java.util.*;
    public class Sanjiaoxing {
        /*
         *    *
         *    **
         *    ***           三角形打印
         */
        // 三角形打印1
        public static void fun1()
        {
            for(int i=0;i<3;i++)
            {
                  
                 for(int j=0;j<i;j++)
                 {
                     System.out.print("*");
                 }
                 System.out.println("*");
                
            }
        }
        //三角形打印如图2
    //      ***
    //      **
    //      *
        public static void fun2()
        {
            for(int i=3;i>0;i--)
            {
                for(int j=i-1;j>0;j--)
                {
                    System.out.print("*");
                }
                System.out.println("*");
                
            }
        }
        // 打印三角形3图形如图
    //      *
    //      **
    //     ***
        
        
        public static void fun3()
        {
            for(int i=0;i<3;i++)
            {
                for(int j=1;j<3-i;j++)
                {
                    System.out.print(" ");
                }
                for(int t=0;t<i;t++)
                {
                  System.out.print("*");
                }
                System.out.println("*");
                
            }
        }
        
         // 打印三角形4如图
    //    ***
    //     **
    //      *
        public static void fun4()
        {
            for(int i=0;i<3;i++)
            {
                for(int j=0;j<i;j++)
                
                    System.out.print(" ");
                    for(int t=1;t<3-i;t++)
                    {
                        System.out.print("*");
                    }
                
                System.out.println("*");
                
            }
        }
     
        public static void main(String[] args)
        {  
            System.out.println("4种不同三角形图案打印如下");
             System.out.print("------------------"+" ");
            fun1();
         System.out.print("------------------"+" ");
            fun2();
            System.out.print("------------------"+" ");
            fun3();
            System.out.print("------------------"+" ");
            fun4();
             System.out.print("------------------"+" ");
        }

    }

       个人自写for的多重循环,利用了4个函数。每个函数分别打印一个图形,最后一起调用4个函数打印出上面的4个三角形。这只是简单的for多重循环的应用,大家如有更多更难的习题可以发过来。

    编程是一门艺术,要爱就要深爱。
  • 相关阅读:
    Gmail、还有人需要吗?
    Google 打不开
    不能忽视的Doctype
    ASP.NET2.0中用ICallbackEventHandler实现客户端与服务器端异步交互
    DataGrid常用小技巧
    ASP.NET程序安全性(三) 表单提交、过滤用户输入
    Objection!!!
    编写3dmax插件需要注意的几个问题
    又一个IGame的bug
    VC2010中的C++0x特性 Part 1:Lambdas,auto, static_assert
  • 原文地址:https://www.cnblogs.com/pwhit/p/5183259.html
Copyright © 2011-2022 走看看