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

    编程是一门艺术,要爱就要深爱。
  • 相关阅读:
    STM32 IIC双机通信—— HAL库硬件IIC版
    利用 ST-LINK Utility软件下载程序
    STM32CubeMx的使用分享
    STM32 GPIO重映射(转)
    IIC 原理讲解
    STM32 软件模拟 IIC 代码,标准库、HAL库可用
    STM32 抢占优先级和响应优先级
    浅谈C中的malloc和free
    C语言-cout<<"123"<<"45"<<endl;
    VC6-Win7下VC++6.0打开多个工程的设置
  • 原文地址:https://www.cnblogs.com/pwhit/p/5183259.html
Copyright © 2011-2022 走看看