Java 中的金字塔模式程序

面试中经常使用模式程序来理解受访者的逻辑思维能力。金字塔模式非常受欢迎,一旦我们了解了它是如何创建的逻辑,写代码来实现相同的任务很容易。

Java 中的金字塔模式程序

Pyramid Pattern Programs in Java Here I am providing some examples to create different pyramid patterns from numbers, symbols etc. We will also look into some examples of creating inverted pyramid pattern in java program. We will try to keep the code simple so that it can be easily understood.

数字的金字塔模式

如果你看第一個模式,每行都包含相同的數字印刷相同的次數. 然而,每行都有領先的白色空白,其數字是行-i

 1package com.journaldev.patterns.pyramid;
 2
 3import java.util.Scanner;
 4
 5public class PyramidPattern {
 6
 7    private static void printPattern1(int rows) {
 8    	// for loop for the rows
 9    	for (int i = 1; i <= rows; i++) {
10    		// white spaces in the front of the numbers
11    		int numberOfWhiteSpaces = rows - i;
12
13    		//print leading white spaces
14    		printString(" ", numberOfWhiteSpaces);
15
16    		//print numbers
17    		printString(i + " ", i);
18
19    		//move to next line
20    		System.out.println("");
21    	}
22    }
23
24    //utility function to print string given times
25    private static void printString(String s, int times) {
26    	for (int j = 0; j < times; j++) {
27    		System.out.print(s);
28    	}
29    }
30
31    public static void main(String[] args) {
32
33    	Scanner scanner = new Scanner(System.in);
34    	System.out.println("Please enter the rows to print:");
35    	int rows = scanner.nextInt();
36    	// System.out.println("Rows = "+rows);
37    	scanner.close();
38
39    	System.out.println("Printing Pattern 1\n");
40    	printPattern1(rows);
41
42    }
43
44}

请注意,我为常见的字符串打印任务创建了一个实用函数. 如果你可以将你的程序分成短的可重复使用的函数,那么它表明你不仅要写程序,还想确保其质量和可重复使用性。

 1Please enter the rows to print:
 29
 3Printing Pattern 1
 4
 5        1 
 6       2 2 
 7      3 3 3 
 8     4 4 4 4 
 9    5 5 5 5 5 
10   6 6 6 6 6 6 
11  7 7 7 7 7 7 7 
12 8 8 8 8 8 8 8 8 
139 9 9 9 9 9 9 9 9

Pyramid Pattern Java Program Output

不断增加的数字的金字塔模式

这里是打印图案的函数 2 要注意的关键点是领先的白色空间的数量,然后数字被打印成日益增加的顺序。

 1/**
 2 * 
 3 * Program to print below pyramid structure
 4 *      1         
 5       1 2        
 6      1 2 3       
 7     1 2 3 4      
 8    1 2 3 4 5     
 9   1 2 3 4 5 6    
10  1 2 3 4 5 6 7   
11 1 2 3 4 5 6 7 8  
121 2 3 4 5 6 7 8 9 
13*/
14private static void printPattern2(int rows) {
15    // for loop for the rows
16    for (int i = 1; i <= rows; i++) {
17    	// white spaces in the front of the numbers
18    	int numberOfWhiteSpaces = rows - i;
19
20    	//print leading white spaces
21    	printString(" ", numberOfWhiteSpaces);
22
23    	//print numbers
24    	for(int x = 1; x<=i; x++) {
25    		System.out.print(x+" ");
26    	}
27
28    	//move to next line
29    	System.out.println("");
30    }
31}

人物的金字塔

这是一个非常简单的,我们只需要打印字符,没有任何计算或操纵。

 1/**
 2 * Program to print following pyramid structure
 3        *         
 4       * *        
 5      * * *       
 6     * * * *      
 7    * * * * *     
 8   * * * * * *    
 9  * * * * * * *   
10 * * * * * * * *  
11* * * * * * * * * 
12
13*/
14private static void printPattern3(int rows) {
15    // for loop for the rows
16    for (int i = 1; i <= rows; i++) {
17    	// white spaces in the front of the numbers
18    	int numberOfWhiteSpaces = rows - i;
19
20    	//print leading white spaces
21    	printString(" ", numberOfWhiteSpaces);
22
23    	//print character
24    	printString("* ", i);
25
26    	//move to next line
27    	System.out.println("");
28    }
29}

金字塔模式4计划

 1/**
 2* 
 3*               1 
 4              1 2 1 
 5            1 2 3 2 1 
 6          1 2 3 4 3 2 1 
 7        1 2 3 4 5 4 3 2 1 
 8      1 2 3 4 5 6 5 4 3 2 1 
 9    1 2 3 4 5 6 7 6 5 4 3 2 1 
10  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
111 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
12*/
13
14private static void printPattern4(int rows) {
15    // for loop for the rows
16    for (int i = 1; i <= rows; i++) {
17    	// white spaces in the front of the numbers
18    	int numberOfWhiteSpaces = (rows-i)*2;
19
20    	//print leading white spaces
21    	printString(" ", numberOfWhiteSpaces);
22
23    	//print numbers
24    	for(int x=1; x0; j--) {
25    		System.out.print(j+" ");
26    	}
27
28    	//move to next line
29    	System.out.println("");
30    }
31}

请注意,每个行都有2*r-1数字,所以我们在打印任何数字之前将有(行-i)*2个白空间,然后数字从1开始到i,然后再到1。

使用Java中的Pyramid Pattern 5

 1/**
 2 * 
 3 *                9 
 4                8 9 8 
 5              7 8 9 8 7 
 6            6 7 8 9 8 7 6 
 7          5 6 7 8 9 8 7 6 5 
 8        4 5 6 7 8 9 8 7 6 5 4 
 9      3 4 5 6 7 8 9 8 7 6 5 4 3 
10    2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 
11  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
12*/
13private static void printPattern5(int rows) {
14    // for loop for the rows
15    for (int i = rows; i >= 1; i--) {
16    	// white spaces in the front of the numbers
17    	int numberOfWhiteSpaces = i*2;
18
19    	//print leading white spaces
20    	printString(" ", numberOfWhiteSpaces);
21
22    	//print numbers
23    	for(int x=i; x=i; j--) {
24    		System.out.print(j+" ");
25    	}
26
27    	//move to next line
28    	System.out.println("");
29    }
30}

反向金字塔字符的模式

以下是反转金字塔程序的代码片段。

 1/**
 2 * 
 3* * * * * * * * * 
 4 * * * * * * * * 
 5  * * * * * * * 
 6   * * * * * * 
 7    * * * * * 
 8     * * * * 
 9      * * * 
10       * * 
11        * 
12 */
13private static void printPattern6(int rows) {
14    // for loop for the rows
15    for (int i = rows; i >= 1; i--) {
16    	// white spaces in the front of the numbers
17    	int numberOfWhiteSpaces = rows - i;
18
19    	//print leading white spaces
20    	printString(" ", numberOfWhiteSpaces);
21
22    	//print character
23    	printString("* ", i);
24
25    	//move to next line
26    	System.out.println("");
27    }
28}

数字的逆转金字塔模式

让我们看看由数字组成的逆转金字塔模式的一个例子。

 1/**
 2     * 
 39 9 9 9 9 9 9 9 9 
 4 8 8 8 8 8 8 8 8 
 5  7 7 7 7 7 7 7 
 6   6 6 6 6 6 6 
 7    5 5 5 5 5 
 8     4 4 4 4 
 9      3 3 3 
10       2 2 
11        1 
12     */
13private static void printPattern7(int rows) {
14    // for loop for the rows
15    for (int i = rows; i >= 1; i--) {
16    	// white spaces in the front of the numbers
17    	int numberOfWhiteSpaces = rows - i;
18
19    	//print leading white spaces
20    	printString(" ", numberOfWhiteSpaces);
21
22    	//print character
23    	printString(i+" ", i);
24
25    	//move to next line
26    	System.out.println("");
27    }
28}

结论

有许多类型的金字塔模式. 最重要的是要了解数字和白色空间的组织模式. 一旦你清楚了这个模式,你可以很容易地在Java或任何其他编程语言中写代码。

进一步阅读

Java Programming Interview QuestionsJava Programming Interview Questions

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

Published At
Categories with 技术
Tagged with
comments powered by Disqus