Java 金字塔模式编程

这篇博文展示了如何编写一个Java程序来打印星号、数字和字母的金字塔模式,并提供了每个的示例。它还涵盖了空心、倒置和倒置空心金字塔模式的最佳方法。

尽管本文展示了打印各种金字塔模式的最佳方法,但我们没有提及代码分析。因为每个子部分都有专门的文章,我们提供了指向它的链接。请点击链接查找替代方法和代码解释。

Java程序打印星号金字塔模式

程序接受用户输入的行数。接下来,我们使用嵌套的for循环来迭代行和列并打印金字塔模式。请参考本文以了解代码。

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Please Enter Rows = ");
		int rw = sc.nextInt();

		for (int i = 1; i <= rw; i++) {
			for (int j = 0; j < rw - i; j++) {
				System.out.print(" ");
			}
			for (int k = 0; k < (i * 2) - 1; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
Java Pyramid Pattern Program

空心金字塔星型图案

有关空心示例的更多信息>>点击这里

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();
		int i, j, k;

		for (i = 1; i <= rw; i++) {
			for (j = 1; j <= rw - i; j++) {
				System.out.print(" ");
			}
			if (i == 1 || i == rw) {
				for (k = 1; k <= (i * 2) - 1; k++) {
					System.out.print("*");
				}
			} else {
				for (k = 1; k <= (i * 2) - 1; k++) {
					if (k == 1 || k == i * 2 - 1) {
						System.out.print("*");
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Rows = 12
           *
          * *
         *   *
        *     *
       *       *
      *         *
     *           *
    *             *
   *               *
  *                 *
 *                   *
***********************

倒金字塔星形图案

有关倒置的更多信息>> 点击这里

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter rows = ");
		int rw = sc.nextInt();

		for (int i = rw; i >= 1; i--) 
		{
			for (int j = 0; j < rw - i; j++) 
			{
				System.out.print(" ");
			}
			for (int k = 0; k != (2 * i) - 1; k++) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter rows = 10
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

星号空心倒金字塔

有关空心倒金字塔的更多信息>> 点击这里

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		for (int i = rw ; i > 0; i-- ) 
		{
			for (int j = 1 ; j <= rw - i; j++ ) 
			{
				System.out.print(" ");
			}
			for (int k = 1 ; k <= (2 * i) - 1; k++ ) 
			{
				if (i == 1 || i == rw || k == 1 || k == (2 * i) - 1)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}			
			}
			System.out.println();
		}
	}
}

输出

Enter Rows = 9
*****************
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *

Java程序打印数字金字塔模式

上述程序打印星号金字塔模式,但您也可以使用数字来填充模式。有关更多信息>> 点击这里

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		for (int i = 1; i <= rw; i++ ) 
		{
			for (int j = rw; j > i; j-- ) 
			{
				System.out.print(" ");
			}
			for(int k = 1; k <= i; k++) 
			{
				System.out.print(i + " ");
			}
			System.out.println();
		}
	}
}

输出

Enter Rows = 9
        1 
       2 2 
      3 3 3 
     4 4 4 4 
    5 5 5 5 5 
   6 6 6 6 6 6 
  7 7 7 7 7 7 7 
 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 

打印字母金字塔模式的程序

此示例程序打印字符或字母来填充模式。有关更多信息>> 点击这里

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		int alp = 64;

		for (int i = 1; i <= rw; i++) 
		{
			for (int j = 1; j <= rw - i; j++) 
			{
				System.out.print(" ");
			}
			for (int k = i; k > 0; k--) 
			{
				System.out.print((char) (alp + k));
			}
			for (int l = 2; l <= i; l++) 
			{
				System.out.print((char) (alp + l));
			}
			System.out.println();
		}
	}
}

输出

Enter Rows = 14
             A
            BAB
           CBABC
          DCBABCD
         EDCBABCDE
        FEDCBABCDEF
       GFEDCBABCDEFG
      HGFEDCBABCDEFGH
     IHGFEDCBABCDEFGHI
    JIHGFEDCBABCDEFGHIJ
   KJIHGFEDCBABCDEFGHIJK
  LKJIHGFEDCBABCDEFGHIJKL
 MLKJIHGFEDCBABCDEFGHIJKLM
NMLKJIHGFEDCBABCDEFGHIJKLMN