Java打印金字塔图案的程序实现

2021年4月11日15:41:41 发表评论 797 次浏览

本文旨在提供用于图案打印的Java实现。

简单的金字塔图案

import java.io.*;
  
//Java code to demonstrate star patterns
public class lsbin
{
     //Function to demonstrate printing pattern
     public static void printStars( int n)
     {
         int i, j;
  
         //outer loop to handle number of rows
         //n in this case
         for (i= 0 ; i<n; i++)
         {
  
             //inner loop to handle number of columns
             //values changing acc. to outer loop    
             for (j= 0 ; j<=i; j++)
             {
                 //printing stars
                 System.out.print( "* " );
             }
  
             //ending line after each row
             System.out.println();
         }
    }
  
     //Driver Function
     public static void main(String args[])
     {
         int n = 5 ;
         printStars(n);
     }
}

输出如下:

* 
* * 
* * * 
* * * * 
* * * * *

180度旋转后

import java.io.*;
  
//Java code to demonstrate star pattern
public class lsbin
{
     //Function to demonstrate printing pattern
     public static void printStars( int n)
     {
         int i, j;
  
         //outer loop to handle number of rows
         //n in this case
         for (i= 0 ; i<n; i++)
         {
  
             //inner loop to handle number spaces
             //values changing acc. to requirement
             for (j= 2 *(n-i); j>= 0 ; j--)
             {
                 //printing spaces
                 System.out.print( " " );
             }
             
             //inner loop to handle number of columns
             //values changing acc. to outer loop
             for (j= 0 ; j<=i; j++)
             {
                 //printing stars
                 System.out.print( "* " );
             }
              
             //ending line after each row
             System.out.println();
         }
     }
  
     //Driver Function
     public static void main(String args[])
     {
         int n = 5 ;
         printStars(n);
     }
}

输出如下:

*
      * * 
    * * * 
  * * * * 
* * * * *

印刷三角

import java.io.*;
  
//Java code to demonstrate star pattern
public class lsbin
{
     //Function to demonstrate printing pattern
     public static void printTriagle( int n)
     {
         //outer loop to handle number of rows
         //n in this case
         for ( int i= 0 ; i<n; i++)
         {
  
             //inner loop to handle number spaces
             //values changing acc. to requirement
             for ( int j=n-i; j> 1 ; j--)
             {
                 //printing spaces
                 System.out.print( " " );
             }
   
             //inner loop to handle number of columns
             //values changing acc. to outer loop
             for ( int j= 0 ; j<=i; j++ )
             {
                 //printing stars
                 System.out.print( "* " );
             }
   
             //ending line after each row
             System.out.println();
         }
     }
      
     //Driver Function
     public static void main(String args[])
     {
         int n = 5 ;
         printTriagle(n);
     }
}

输出如下:

* 
   * * 
  * * * 
 * * * * 
* * * * *

数字模式

import java.io.*;
  
//Java code to demonstrate number pattern
public class lsbin
{
     //Function to demonstrate printing pattern
     public static void printNums( int n)
     {
         int i, j, num;
  
         //outer loop to handle number of rows
         //n in this case
         for (i= 0 ; i<n; i++)
         {
             //initialising starting number
             num= 1 ;
  
             //inner loop to handle number of columns
             //values changing acc. to outer loop
             for (j= 0 ; j<=i; j++)
             {
                 //printing num with a space 
                 System.out.print(num+ " " );
  
                 //incrementing value of num
                 num++;
             }
  
             //ending line after each row
             System.out.println();
         }
     }
  
     //Driver Function
     public static void main(String args[])
     {
         int n = 5 ;
         printNums(n);
     }
}

输出如下:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

没有重新分配的号码

import java.io.*;
  
//Java code to demonstrate star pattern
public class lsbin
{
     //Function to demonstrate printing pattern
     public static void printNums( int n)
     {
         //initialising starting number
         int i, j, num= 1 ;
          
         //outer loop to handle number of rows
         //n in this case
         for (i= 0 ; i<n; i++)
         {
  
             //without re assigning num
             //num = 1;
             for (j= 0 ; j<=i; j++)
             {
                 //printing num with a space
                 System.out.print(num+ " " );
                  
                 //incrementing num at each column
                 num = num + 1 ;
             }
  
             //ending line after each row
             System.out.println();
         }
     }
      
     //Driver Function
     public static void main(String args[])
     {
         int n = 5 ;
         printNums(n);
     }
}

输出如下:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: