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

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

打印金字塔星形图案的Java程序

本文将指导您完成在Java中打印金字塔星形图案的过程。

1.简单的金字塔图案

Java
import java.io.*;
  
// Java code to demonstrate Pyramid star patterns
public class GeeksForGeeks {
    // Function to demonstrate printing pattern
    public static void PyramidStar(int n)
    {
        int a, b;
  
        // outer loop to handle number of rows
        // k in this case
        for (a = 0; a < n; a++) {
  
            // inner loop to handle number of columns
            // values changing acc. to outer loop
            for (b = 0; b <= a; b++) {
                // printing stars
                System.out.print("* ");
            }
  
            // end-line
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int k = 5;
        PyramidStar(k);
    }
}


Java
import java.io.*;
  
// 180 flipped pyramid star pattern
public class GFG {
    // Function to demonstrate printing pattern
    public static void FlippedPyramidStar(int k)
    {
        int a, b;
  
        // 1st loop
        for (a = 0; a < k; a++) {
  
            // nested 2nd loop
            for (b = 2 * (k - a); b >= 0; b--) {
                // printing spaces
                System.out.print(" ");
            }
  
            // nested 3rd loop
            for (b = 0; b <= a; b++) {
                // printing stars
                System.out.print("* ");
            }
  
            // end-line
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int k = 5;
        FlippedPyramidStar(k);
    }
}


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


输出
* 
* * 
* * * 
* * * * 
* * * * * 

2. 180度旋转后/镜像模式

在这里,我们将打印一个旋转 180 度的星形金字塔。



Java

import java.io.*;
  
// 180 flipped pyramid star pattern
public class GFG {
    // Function to demonstrate printing pattern
    public static void FlippedPyramidStar(int k)
    {
        int a, b;
  
        // 1st loop
        for (a = 0; a < k; a++) {
  
            // nested 2nd loop
            for (b = 2 * (k - a); b >= 0; b--) {
                // printing spaces
                System.out.print(" ");
            }
  
            // nested 3rd loop
            for (b = 0; b <= a; b++) {
                // printing stars
                System.out.print("* ");
            }
  
            // end-line
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int k = 5;
        FlippedPyramidStar(k);
    }
}
输出
* 
         * * 
       * * * 
     * * * * 
   * * * * * 

3. 打印三角形:

Java

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