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多重循环的应用,大家如有更多更难的习题可以发过来。

    编程是一门艺术,要爱就要深爱。
  • 相关阅读:
    HDU 4379 水题,大水,但我WA了很多次,做了很久
    HDU 1712分组背包 考试复习安排
    所谓的二维背包Triangular Pastures POJ 1948
    ZOJ 1203 Swordfish Kruskal算法求最小生成树
    POJ 2576 Tug of War二维背包恰好装满.
    O(n*m)复杂度的多重背包coinsPOJ 1742
    拓扑排序POJ 1094
    页面右键下拉表
    gb2312 unicode转换工具
    INPUT读出URL里的变量名称
  • 原文地址:https://www.cnblogs.com/pwhit/p/5183259.html
Copyright © 2011-2022 走看看