Javaプログラムでピラミッドスターパターンを印刷する
この記事では、ピラミッド型の星型パターンを印刷する方法を理解します。パターンは、複数のforループとprintステートメントを使用して形成されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the number of rows : 8
出力
必要な出力は-
になりますThe pyramid star pattern :
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * * アルゴリズム
Step 1 - START Step 2 - Declare three integer values namely i, j 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 Pyramid{
public static void main(String args[]){
int i, j, 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 pyramid star pattern : ");
for (i=0; i<my_input; i++){
for (j=my_input-i; j>1; j--){
System.out.print(" ");
}
for (j=0; j<=i; 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 pyramid star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class Pyramid{
public static void main(String args[]){
int i, j, my_input;
my_input = 8;
System.out.println("The number of rows is defined as " +my_input);
System.out.println("The pyramid star pattern : ");
for (i=0; i<my_input; i++){
for (j=my_input-i; j>1; j--){
System.out.print(" ");
}
for (j=0; j<=i; j++ ){
System.out.print("* ");
}
System.out.println();
}
}
} 出力
The number of rows is defined as 8
The pyramid star pattern :
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * * -
文字列を出力するJavaプログラム
この記事では、Javaで文字列を出力する方法を理解します。文字列は、文字と英数字の値のパターンです。文字列を作成する最も簡単な方法は、次のように書くことです- String str = "Welcome to the club!!!" コード内で文字列リテラルが検出されると、コンパイラはその値(この場合は「クラブへようこそ!!!」)を使用してStringオブジェクトを作成します。 他のオブジェクトと同様に、newキーワードとコンストラクターを使用してStringオブジェクトを作成できます。 Stringクラスには11個のコンストラクタがあり、文字の配列など、さまざまなソ
-
反転した星のパターンを印刷する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): &