ミラーの下部の星の三角形のパターンを印刷するJavaプログラム
この記事では、鏡の下の星型の三角形のパターンを印刷する方法を理解します。パターンは、複数のforループとprintステートメントを使用して形成されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the number of rows : 8
出力
必要な出力は-
になりますThe mirror lower star triangle pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
アルゴリズム
Step 1 - START Step 2 - Declare four integer values namely i, j,k and my_input Step 3 - Read the required values from the user/ define the values Step 4 - We iterate through two nested 'for' loops to get space between the characters. Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character. Step 6 - Now, print a newline to get the specific number of characters in the subsequent lines. Step 7 - Display the result Step 8 - Stop
例1
ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます
。
import java.util.Scanner;
public class MirrorTriangle{
public static void main(String args[]){
int i, j, k, my_input;
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 Mirror Lower Star Triangle pattern : ");
for (i = 1; i <= my_input; i++) {
for (j = 1; j < i; j++) {
System.out.print(" ");
}
for (j = i; j <= my_input; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
for (i = my_input - 1; i >= 0; i--) {
for (j = 0; j < i; j++) {
System.out.print(" ");
}
for (j = i; j <= my_input - 1; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
} 出力
Required packages have been imported A reader object has been defined Enter the number of rows : 8 The Mirror Lower Star Triangle pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *>
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class MirrorTriangle{
public static void main(String args[]){
int i, j, k, my_input;
my_input = 8;
System.out.println("The number of rows is defined as " +my_input);
System.out.println("The mirror lower star triangle pattern : ");
for (i = 1; i <= my_input; i++) {
for (j = 1; j < i; j++) {
System.out.print(" ");
}
for (j = i; j <= my_input; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
for (i = my_input - 1; i >= 0; i--) {
for (j = 0; j < i; j++) {
System.out.print(" ");
}
for (j = i; j <= my_input - 1; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
} 出力
The number of rows is defined as 8
The mirror lower star triangle pattern :
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * * -
Javaでの三角形パターンの印刷
以下は、三角形のパターンを印刷するJavaプログラムです- 例 import java.util.*; public class Demo{ public static void main(String[] args){ Scanner my_scan = new Scanner(System.in); System.out.println("Enter the number of rows which needs to be printed"); &
-
反転した星のパターンを印刷するPythonプログラム
Pythonで逆スターパターンを印刷する必要がある場合は、「for」ループを使用できます。これにより、数値の範囲を反復処理し、必要な文字を必要な頻度で出力できます。反復ごとにカウントを減らすことができます。 以下は同じのデモンストレーションです- 例 N=6 print("The value of 'N' has been initialized to "+str(N)) print("The inverted stars are being displayed") for i in range (N, 0, -1): &