이 기사에서는 피라미드와 패턴을 만드는 방법을 이해할 것입니다. 패턴은 여러 for 루프와 인쇄 문을 사용하여 형성됩니다.
아래는 동일한 데모입니다 -
입력
입력이 -
라고 가정합니다.The number of rows is defined as 8
출력
원하는 출력은 -
The pyramid pattern : 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8
알고리즘
Step 1 - START Step 2 - Declare six integer values namely my_input, k, i, my_count, my_temp and my_spaces. Step 3 - Read the required values from the user/ define the values Step 4 - Assign value of 0 to ‘k’, my_count and my_temp Step 5 - We iterate through two nested 'for' loops to get space between the characters. Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the value of (i + k - 2 * my_temp). Step 7 - Now, print a newline to get the specific number of characters in the subsequent lines. Step 8 - Display the result Step 9 - Stop
예시 1
여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 코딩 기반 도구에서 이 예제를 라이브로 사용해 볼 수 있습니다. .
import java.util.Scanner; public class Pyramid { public static void main(String[] args) { int my_input, k, i, my_count, my_temp, my_spaces; my_input = 8; k = 0; my_count = 0; my_temp = 0; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number of rows: "); my_input = my_scanner.nextInt(); System.out.println("The pyramid pattern : "); for ( i = 1; i <= my_input; ++i) { for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) { System.out.print(" "); ++my_count; } while (k != 2 * i - 1) { if (my_count <= my_input - 1) { System.out.print((i + k) + " "); ++my_count; } else { ++my_temp; System.out.print((i + k - 2 * my_temp) + " "); } ++k; } my_temp = my_count = k = 0; System.out.println(); } } }
출력
Required packages have been imported A reader object has been defined Enter the number of rows: 8 The pyramid pattern : 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8
예시 2
여기에서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.
public class Pyramid { public static void main(String[] args) { int my_input, k, i, my_count, my_temp, my_spaces; my_input = 8; k = 0; my_count = 0; my_temp = 0; System.out.println("The number of rows is defined as " +my_input); System.out.println("The pyramid pattern : "); for ( i = 1; i <= my_input; ++i) { for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) { System.out.print(" "); ++my_count; } while (k != 2 * i - 1) { if (my_count <= my_input - 1) { System.out.print((i + k) + " "); ++my_count; } else { ++my_temp; System.out.print((i + k - 2 * my_temp) + " "); } ++k; } my_temp = my_count = k = 0; System.out.println(); } } }
출력
The number of rows is defined as 8 The pyramid pattern : 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8