📜  用Java打印金字塔图案的程序

📅  最后修改于: 2022-05-13 01:54:46.263000             🧑  作者: Mango

用Java打印金字塔图案的程序

本文旨在为图案打印提供Java实现。

  • 简单的金字塔图案
Java
import java.io.*;
 
// Java code to demonstrate star patterns
public class GeeksForGeeks
{
    // 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


Java
import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // 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=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);
    }
}


Java
import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printTriangle(int n)
    {
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i1; 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;
        printTriangle(n);
    }
}


Java
//MainFunction
public class ReversePyramid
{
    public static void main(String[] args)
    {
        int rows = 6; // Number of Rows we want to print
          
         
          
        //Printing the pattern
        for (int i = 1; i <= rows; i++)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
        
 
        
        }
     
    }


Java
//MainFunction
public class ReversePattern
{
    public static void main(String[] args)
    {
        int rows = 7; // Number of Rows we want to print
          
         
          
        //Printing the pattern
        for (int i = 1; i <= rows; i++)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
        
 
       //Printing the reverse pattern
        for (int i = rows-1; i >= 1; i--)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            } 
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
     
    }
}


Java
import java.io.*;
 
// Java code to demonstrate number pattern
public class GeeksForGeeks
{
    // 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


Java
import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // 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


Java
class  PrintChristmasTree{
 
    //Value 5 is permanently provided to height variable
  public static final int height = 5;
   
   //Main Function
  public static void main(String[] args) {
     
    //Assigning Width
    int width = 5;
 
    //Assigning Space
    int space = width*5;
 
    int x = 1;
 
    //Code to Print Upper Part of the Tree i.e. Pyramids.
    for(int a = 1;a <= height ;a++){
 
      for(int i = x;i <= width;i++){
 
        for(int j = space;j >= i;j--){
 
          System.out.print(" ");
        }
 
        for(int k = 1;k <= i;k++){
 
          System.out.print("* ");
        }
 
        System.out.println();
      }
 
      x = x+2;
      width = width+2;
    }
 
 
    //Printing  Branch of Christmas Tree
    for(int i = 1;i <= 4;i++){
 
      for(int j = space-3;j >= 1;j--){
         
        System.out.print(" ");
      }
 
      for(int k= 1;k <= 4;k++){
        System.out.print("* ");
      }
 
      System.out.println();
    }
  }
}


输出
* 
* * 
* * * 
* * * * 
* * * * * 
  • 180度旋转后

Java

import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // 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=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);
    }
}
输出
* 
         * * 
       * * * 
     * * * * 
   * * * * * 
  • 印刷三角

Java

import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printTriangle(int n)
    {
        // outer loop to handle number of rows
        //  n in this case
        for (int i=0; i1; 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;
        printTriangle(n);
    }
}
输出
* 
   * * 
  * * * 
 * * * * 
* * * * * 
  • 打印金字塔的反面

Java

//MainFunction
public class ReversePyramid
{
    public static void main(String[] args)
    {
        int rows = 6; // Number of Rows we want to print
          
         
          
        //Printing the pattern
        for (int i = 1; i <= rows; i++)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
        
 
        
        }
     
    }
输出
1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 
  • 带镜像的数字图案

Java

//MainFunction
public class ReversePattern
{
    public static void main(String[] args)
    {
        int rows = 7; // Number of Rows we want to print
          
         
          
        //Printing the pattern
        for (int i = 1; i <= rows; i++)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
        
 
       //Printing the reverse pattern
        for (int i = rows-1; i >= 1; i--)
        {
          for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            } 
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }  
            System.out.println();
        }
     
    }
}
输出
1 2 3 4 5 6 7 
 2 3 4 5 6 7 
  3 4 5 6 7 
   4 5 6 7 
    5 6 7 
     6 7 
      7 
     6 7 
    5 6 7 
   4 5 6 7 
  3 4 5 6 7 
 2 3 4 5 6 7 
1 2 3 4 5 6 7 
  • 数字模式

Java

import java.io.*;
 
// Java code to demonstrate number pattern
public class GeeksForGeeks
{
    // 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
输出
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
  • 无需重新分配的数字

Java

import java.io.*;
 
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
    // 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
输出
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
  • 使用 Pyramid 打印圣诞树

Java

class  PrintChristmasTree{
 
    //Value 5 is permanently provided to height variable
  public static final int height = 5;
   
   //Main Function
  public static void main(String[] args) {
     
    //Assigning Width
    int width = 5;
 
    //Assigning Space
    int space = width*5;
 
    int x = 1;
 
    //Code to Print Upper Part of the Tree i.e. Pyramids.
    for(int a = 1;a <= height ;a++){
 
      for(int i = x;i <= width;i++){
 
        for(int j = space;j >= i;j--){
 
          System.out.print(" ");
        }
 
        for(int k = 1;k <= i;k++){
 
          System.out.print("* ");
        }
 
        System.out.println();
      }
 
      x = x+2;
      width = width+2;
    }
 
 
    //Printing  Branch of Christmas Tree
    for(int i = 1;i <= 4;i++){
 
      for(int j = space-3;j >= 1;j--){
         
        System.out.print(" ");
      }
 
      for(int k= 1;k <= 4;k++){
        System.out.print("* ");
      }
 
      System.out.println();
    }
  }
}
输出
* 
                        * * 
                       * * * 
                      * * * * 
                     * * * * * 
                       * * * 
                      * * * * 
                     * * * * * 
                    * * * * * * 
                   * * * * * * * 
                     * * * * * 
                    * * * * * * 
                   * * * * * * * 
                  * * * * * * * * 
                 * * * * * * * * * 
                   * * * * * * * 
                  * * * * * * * * 
                 * * * * * * * * * 
                * * * * * * * * * * 
               * * * * * * * * * * * 
                 * * * * * * * * * 
                * * * * * * * * * * 
               * * * * * * * * * * * 
              * * * * * * * * * * * * 
             * * * * * * * * * * * * * 
                      * * * * 
                      * * * * 
                      * * * * 
                      * * * *