左の三角形の星のパターンを印刷するJavaプログラム
この記事では、左三角形の星型を印刷する方法を理解します。パターンは、複数のforループとprintステートメントを使用して形成されます。
以下は同じのデモンストレーションです-
入力
入力が-
であると仮定しますEnter the number of rows : 8
出力
必要な出力は-
になりますThe right triangle 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 RightTriangle{ 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 right triangle star pattern : "); for (i=0; i<my_input; i++){ for (j=2*(my_input-i); j>=0; 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 right triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
例2
ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。
public class RightTriangle{ 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 right triangle star pattern : "); for (i=0; i<my_input; i++){ for (j=2*(my_input-i); j>=0; 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 right triangle star pattern : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
Java 9のJShellで星のパターンを印刷するにはどうすればよいですか?
JShell は、Java 9で導入されたREPLツールであり、Javaコードを実行して結果をすぐに取得できるようにします。 評価できます 表現 または、新しいプロジェクトを作成せずに単純なアルゴリズムを作成し、JShellを使用してコンパイルまたはビルドします。式を実行したり、インポートを使用したり、クラス、メソッド、変数を定義したりすることもできます。これはJava9JDKの一部ですが、JREではありません。 jshell と入力するだけで、コマンドプロンプトでJShellセッションを開始できます。 。さまざまなコマンドを使用できます: / exit JShellセッションを終了す
-
パターン「G」を印刷するPythonプログラム
「*」を使用して文字「G」のパターンを印刷する必要がある場合は、メソッドを定義し、ネストされたループを使用して数値を反復処理し、「*」を印刷して「G」パターンを形成できます。 以下は同じのデモンストレーションです- 例 def display_pattern(my_line): my_pattern="" for i in range(0,my_line): for j in range(0,my_line):