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