Java中的金字塔模式程序

模式程序在面试中被广泛使用,以了解面试者的逻辑思维能力。金字塔模式非常流行,一旦我们理解了它的创建方式,编写代码实现相同效果就变得简单了。

Java中的金字塔模式程序

这里我提供一些例子,展示如何从数字、符号等创建不同的金字塔模式。我们还将看一些创建倒金字塔模式的Java程序的示例。我们会尽量保持代码简单易懂。

数字的金字塔模式

如果你看第一个模式,每一行都包含相同次数打印的相同数字。但是,每一行都有一些前导空格,其数量为“行数-i”。让我们来看一下打印此模式的程序。

package com.journaldev.patterns.pyramid;

import java.util.Scanner;

public class PyramidPattern {

	private static void printPattern1(int rows) {
		// 循环行数
		for (int i = 1; i <= rows; i++) {
			// 数字前的空格
			int numberOfWhiteSpaces = rows - i;

			// 打印前导空格
			printString(" ", numberOfWhiteSpaces);

			// 打印数字
			printString(i + " ", i);

			// 移动到下一行
			System.out.println("");
		}
	}

	// 给定次数打印字符串的实用函数
	private static void printString(String s, int times) {
		for (int j = 0; j < times; j++) {
			System.out.print(s);
		}
	}

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter the rows to print:");
		int rows = scanner.nextInt();
		// System.out.println("行数 = "+rows);
		scanner.close();

		System.out.println("Printing Pattern 1\n");
		printPattern1(rows);

	}

}

请注意,我创建了一个用于常见字符串打印任务的实用函数。如果你能将你的程序划分为短小的可重用函数,那么这表明你不仅想要编写程序,还想确保其质量和可重用性。当我们运行上述程序时,我们得到以下输出。

Please enter the rows to print:
9
Printing Pattern 1

        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 
Pyramid Pattern Java Program Output

递增数字的金字塔图案

这是打印模式2的函数。要注意的关键点是前导空格的数量,然后数字以递增顺序打印。

/**
 * 
 * Program to print below pyramid structure
 *      1         
       1 2        
      1 2 3       
     1 2 3 4      
    1 2 3 4 5     
   1 2 3 4 5 6    
  1 2 3 4 5 6 7   
 1 2 3 4 5 6 7 8  
1 2 3 4 5 6 7 8 9 
*/
private static void printPattern2(int rows) {
	// 循环行数
	for (int i = 1; i <= rows; i++) {
		// 数字前的空格
		int numberOfWhiteSpaces = rows - i;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印数字
		for(int x = 1; x<=i; x++) {
			System.out.print(x+" ");
		}

		// 移动到下一行
		System.out.println("");
	}
}

字符金字塔

这是一个非常简单的例子,我们只需打印字符而不进行任何计算或操作。

/**
 * Program to print following pyramid structure
        *         
       * *        
      * * *       
     * * * *      
    * * * * *     
   * * * * * *    
  * * * * * * *   
 * * * * * * * *  
* * * * * * * * * 

*/
private static void printPattern3(int rows) {
	// 用于行的循环
	for (int i = 1; i <= rows; i++) {
		// 数字前面的空格
		int numberOfWhiteSpaces = rows - i;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印字符
		printString("* ", i);

		// 移动到下一行
		System.out.println("");
	}
}

金字塔图案 4 程序

/**
* 
*               1 
              1 2 1 
            1 2 3 2 1 
          1 2 3 4 3 2 1 
        1 2 3 4 5 4 3 2 1 
      1 2 3 4 5 6 5 4 3 2 1 
    1 2 3 4 5 6 7 6 5 4 3 2 1 
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
*/

private static void printPattern4(int rows) {
	// 用于行的循环
	for (int i = 1; i <= rows; i++) {
		// 数字前面的空格
		int numberOfWhiteSpaces = (rows-i)*2;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印数字
		for(int x=1; x0; j--) {
			System.out.print(j+" ");
		}

		// 移动到下一行
		System.out.println("");
	}
}

请注意,每一行都有2*r-1个数字。所以在打印任何数字之前,我们将有(rows-i)*2个空格。然后数字从1到“i”,然后再次到1。我们打印数字的逻辑需要两个for循环来实现。

Java 中的金字塔图案 5 程序

/**
 * 
 *                9 
                8 9 8 
              7 8 9 8 7 
            6 7 8 9 8 7 6 
          5 6 7 8 9 8 7 6 5 
        4 5 6 7 8 9 8 7 6 5 4 
      3 4 5 6 7 8 9 8 7 6 5 4 3 
    2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 
  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
*/
private static void printPattern5(int rows) {
	// 用于行的循环
	for (int i = rows; i >= 1; i--) {
		// 数字前面的空格
		int numberOfWhiteSpaces = i*2;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印数字
		for(int x=i; x=i; j--) {
			System.out.print(j+" ");
		}

		// 移动到下一行
		System.out.println("");
	}
}

字符的倒金字塔模式

这是字符倒金字塔程序的代码片段。

/**
 * 
* * * * * * * * * 
 * * * * * * * * 
  * * * * * * * 
   * * * * * * 
    * * * * * 
     * * * * 
      * * * 
       * * 
        * 
 */
private static void printPattern6(int rows) {
	// 用于行的循环
	for (int i = rows; i >= 1; i--) {
		// 数字前面的空格
		int numberOfWhiteSpaces = rows - i;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印字符
		printString("* ", i);

		// 移动到下一行
		System.out.println("");
	}
}

数字的倒金字塔模式

让我们看一个由数字制成的倒金字塔模式的示例。

	/**
	 * 
9 9 9 9 9 9 9 9 9 
 8 8 8 8 8 8 8 8 
  7 7 7 7 7 7 7 
   6 6 6 6 6 6 
    5 5 5 5 5 
     4 4 4 4 
      3 3 3 
       2 2 
        1 
	 */
private static void printPattern7(int rows) {
	// 用于行的循环
	for (int i = rows; i >= 1; i--) {
		// 数字前面的空格
		int numberOfWhiteSpaces = rows - i;

		// 打印前导空格
		printString(" ", numberOfWhiteSpaces);

		// 打印字符
		printString(i+" ", i);

		// 移动到下一行
		System.out.println("");
	}
}

结论

有许多种金字塔模式。最重要的一点是理解数字和空格组织的模式。一旦你清楚了模式,你就可以在Java或任何其他编程语言中轻松编写代码。如果你正在寻找特定的模式程序,请告诉我,我会尽力帮助你。

进一步阅读

Java中的字符串程序Java编程面试问题

你可以从我们的GitHub存储库中查看完整的代码和更多的编程示例。

Source:
https://www.digitalocean.com/community/tutorials/pyramid-pattern-programs-in-java