目次
Java のパターンの例
例 1: 数字を使用してピラミッドの半分を印刷します。
例 2: 数字の矢印を印刷します。
例 3: 星(*) を使用して完全なピラミッドを印刷します。
例 4: 数字を使用して半逆ピラミッドを印刷します。
例 5: アルファベットを使用してピラミッドの半分を印刷します。
例6: アルファベットの印刷パターン
例 7: 星 (*) を使用して正方形を印刷します。
Example 8: Printing rectangle using stars (*).
Example 9: Printing a Diamond using stars.
Example 10: Printing binary numbers in a stair format.
Example 11: Program to print repeating alphabet patterns.
Conclusion

Java のパターン

Aug 30, 2024 pm 04:24 PM
java

記事「Java のパターン」では、Java のプログラミング言語を学習し、高度な概念に深く入る前に、ループの仕組みを理解することが重要です。ループには for、while、do-while ループの 3 種類があります。各ループは互いにわずかに異なるため、プログラムの特定の状況に応じて使用されます。さまざまなループを使用するには、何らかのプログラミング ロジックが必要であり、そのために論理的思考力を必要とするパターン練習がプログラマーに与えられます。たとえば、幾何学的図形 (三角形、四角形など)、ピラミッド、星、数字、文字スタイルのさまざまなパターンのボックスをコンソール画面に印刷できます。ループの形式や基本構文はプログラミング言語によって異なる場合がありますが、これらのパターンを出力する一般的なロジックは同じです。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

Java のパターンの例

いくつかの例を通して Java でパターンを描画する方法を理解しましょう

例 1: 数字を使用してピラミッドの半分を印刷します。

コード:

public class Pyramid
{
public static void main(String[] args)
{
int i, j;
​//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++)
{
​//innermost loop is to print the numbers in the specific rows for (j=1; j<=i; j++)
{
System.out.print(j +" " );
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

上記の例では、パターンを印刷するために必要な基本ループは 2 つだけです。最初の for ループは行数を指定します。この場合、行 (つまり 5 つ) を定義しました。それ以外の場合は、ユーザーからの入力を取得して変数に格納することもできます。内側のループは、特定の行の数値を出力します。 1 行の完了または ‘j’ ループの終了後、println() を使用して行が変更されます。

例 2: 数字の矢印を印刷します。

コード:

public class NumberTriangle
{
public static void main(String[] args)
{
int i, j;
int rows =7;
​//outermost loop to represent the number of rows which is 7 in this case
//for the upper half of arrow
for (i=1; i<= rows; i++)
{
​//innermost loop is to print the numbers in the specific rows
//for the upper half of arrow
for (j=1; j<=i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
​//outermost loop to represent the number of rows which is 6 in this case
//for the lower half of arrow
for (i=rows-1; i>=1; i--)
{
​//innermost loop is to print the numbers in the specific rows
//for the lower half of arrow
for (j=1; j<=i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

上記の例では、矢印を 2 つの半分に分割し、それぞれの半分に 2 つのループを使用する必要があります。行の前半は行に設定された初期値になりますが、行数は下半分の初期値より 1 減ります。両方の半分の内側のループは、外側のループに従って各行を反復処理するために使用されます。

例 3: 星(*) を使用して完全なピラミッドを印刷します。

コード:

public class FullPyramid
{
public static void main(String[] args)
{
int i, j, k;
int rows = 5;
//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= rows; i++)
{
//innermost loop to represent the spaces in pyramid for (j= 1; j<= rows-i; j++)
{
System.out.print(" ");
}
​//innermost loop to represent the stars (*) in pyramid for (k= 1; k<= 2*i-1; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

上記の例では、3 つのことを行う必要があります。つまり、最初の for ループが 1 から rows 変数まで動作するピラミッドの出力の合計行数を念頭に置きます。次に、最初にピラミッド内のスペースを印刷し、次にスペースの後にパターン (*) を印刷する必要があります。この 2 番目と 3 番目では、外側のループ「i」内で for ループが使用されます。

例 4: 数字を使用して半逆ピラミッドを印刷します。

コード:

public class ReversePyramid
{
public static void main(String[] args)
{
int i, j, k;
int rows = 5;
​//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= rows; i++)
{
//innermost loop to represent the spaces
for (j= 1; j<= rows-1; j++)
{
System.out.print(" ");
}
​//innermost loop to represent the stars (*) in pyramid for (k= 1; k<= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

単純なハーフピラミッドは、数字、*、または印刷する文字を処理する必要があるため簡単ですが、逆ピラミッドの場合は、最初にスペースを印刷し、次にパターンを印刷する必要があります。この場合、これは (*) です。 。したがって、3 つの for ループが使用され、完全なピラミッドの場合と同様に機能します。

例 5: アルファベットを使用してピラミッドの半分を印刷します。

コード:

public class AlphabetPyramid
{
public static void main(String[] args)
{
int i, j;
​//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++)
{
int ch = 65;
​//innermost loop to represent the alphabets in a pyramid in particular row for (j= 1; j<= i; j++)
{
System.out.print((char)(ch + i - 1) + " ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

ピラミッドは、上の例で使用したものと同じロジックで出力されます。2 つの for ループを使用し、1 つは行数用、もう 1 つは特定の行の文字出力用です。ただし、最も注意しなければならないのは文字データの扱いです。たとえば、Java では「A」の数値は 65 なので、すべての数学的論理はアルファベットの数値を使用して実行され、最終的には文字形式で出力されます。

例6: アルファベットの印刷パターン

コード:

public class AlphabetPattern
{
public static void main(String[] args)
{
int i, j;
//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++)
{
int ch = 65;
​//innermost loop to represent the alphabets for (j= 1; j<= i; j++)
{
System.out.print((char)(ch - 1 + j) + " ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

上記の例の文字値と 2 つの for ループを処理するために従う基本パターンは例 5 と似ていますが、唯一の違いは、目的のパターンを出力するために使用される単純なロジックです。

例 7: 星 (*) を使用して正方形を印刷します。

コード:

public class SquarePattern
{
public static void main(String[] args)
{
int i, j;
​//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++)
{
int ch = 65;
//innermost loop to represent the stars (*) for (j= 1; j<= 5; j++)
{
System.out.print(" * " + " ");
}
System.out.println();
}
}
}
ログイン後にコピー

出力:

Java のパターン

For printing of square, we need length and width, i.e. both sides of the square should be the same, which is 5 in our case. So the first ​ ​loop is used for the length or number of rows in the square, and the inner ​ ​loop is used for the width of the square, i.e. 5 stars in a single row.

Example 8: Printing rectangle using stars (*).

Code:

public class RectanglePattern
{
public static void main(String[] args)
{
int i, j;
​//outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++)
{
int ch = 65;
​//innermost loop to represent columns the stars (*) for (j= 1; j<= 9; j++)
{
System.out.print(" * " + " " );
}
System.out.println();
}
}
}
ログイン後にコピー

Output:

Java のパターン

The basic logic of printing the rectangle of (*) is the same as printing of squares, the only difference between is the different length and width of the rectangle. Here ‘i’ loop is for the length of the rectangle, and the inner ‘j’ loop is for the width of the loop. Our program is taken as a constant value; we can also ask the user and store them in separate variables.

Example 9: Printing a Diamond using stars.

Printing a diamond in Java is a very simple process. It involves printing 2 pyramids, 1 in the upward direction and another in an inverted direction. Basically, we need to use the loops to do the coding to print two separate pyramids.

Code:

public class Diamond
{
public static void main(String[] args)
{
int i, j, k;
int rows = 5;
​//outermost loop to represent the number of rows which is 5 in this case.
// Creating upper pyramid
for(i= 1; i<= rows; i++)
{
//innermost loop to represent the spaces in upper pyramid for (j= 1; j<= rows-i; j++)
{
System.out.print(" ");
}
​//innermost loop to represent the stars (*) in upper pyramid for (k= 1; k<= 2*i-1; k++)
{
System.out.print("* ");
}
System.out.println();
}
​//outermost loop for the rows in the inverted pyramid for (i = rows-1; i>0; i--)
{
​//innermost loop for the space present in the inverted pyramid for (j=1; j<= rows - i; j++)
{
System.out.print(" ");
}
​//innermost loop inside the outer loop to print the ( * ) pattern in inverted pyramid for (k = 1; k<= 2*i-1; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
ログイン後にコピー

In the above example, almost the same logic is applied to create both pyramids, one in an upward direction and another in an inverted direction. Thus, the first ​loop is for the number of lines or rows in the pattern, and the second is for spaces and the stars (*) pattern in the pattern.

Output:

Java のパターン

Example 10: Printing binary numbers in a stair format.

Code:

public class BinaryStair
{
public static void main(String[] args)
{
int i, j;
//outer loop for the total rows which is 5 in this case for (i = 1; i <= 5; i++)
{
​//inner loop for the pattern of 0 and 1 in each row for (j = 1; j<= i ; j++)
{
if (j % 2 ==0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
}
}
ログイン後にコピー

Output:

Java のパターン

In the above example, in order to print binary pattern, outer ​for ​loop ‘i’ is used for a total number of rows, and the inner ​for ​loop ‘j’ is used to iterate till the outer loop ‘i’ because for the 1st row, we need 1 value, for the 2nd row we need 2 values, and so on. ​If​ and else ​statements are used in order to print the alternate value of 0 and 1. Suppose for the first time i=1, j=1 and 1%2 != 0, then 1 is printed, and execution will move out of the inner loop.

Example 11: Program to print repeating alphabet patterns.

Code:

public class AlphabetReverseOrder
{
public static void main(String[] args)
{
int i, j, k;
//outer loop for the total rows which is 5 in this case for (i = 0 ; i<=5; i++)
{
int ch= 65;
//inner loop for the pattern of alphabets in till ‘i’ loop for (j = 0; j <=i ; j++)
{
System.out.print((char) (ch+j) + " ");
}
//inner loop for the pattern of alphabets in reverse order from ‘i’ loop for (k= i-1; k >=0; k--)
{
System.out.print((char) (ch+k) + " ");
}
System.out.println();
}
}
}
ログイン後にコピー

Output:

Java のパターン

In the above example, if we observe each row of pattern, we need to print the alphabet first in the increasing order, i.e. A B and then in the reverse order, i.e. A B A. For this, we need 3 loops, 1st ​for​ loop for the total number of rows. 2nd ​for​ loop to print the alphabets in increasing order then the 3rd ​for​ loop which remains inside the outer ‘i’ loop and prints the alphabets in the same line but in reverse order of ‘j’ loop.

Conclusion

The above example and their explanations clearly show how to make such patterns in Java. Though these patterns seem to be difficult in the starting, observing them deeply of how the repetition of pattern is happening in a single row and according to how many loops should be used, it becomes easy to do hands-on on this. Today also, in interviews of big companies, candidates are asked to write the logic of patterns of varying difficulty levels because this pattern making shows the basic logical and programming knowledge of an individual.

以上がJava のパターンの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Java 8 Stream Foreachから休憩または戻ってきますか? Java 8 Stream Foreachから休憩または戻ってきますか? Feb 07, 2025 pm 12:09 PM

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

PHP:Web開発の重要な言語 PHP:Web開発の重要な言語 Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHP対Python:違いを理解します PHP対Python:違いを理解します Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP対その他の言語:比較 PHP対その他の言語:比較 Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHP対Python:コア機能と機能 PHP対Python:コア機能と機能 Apr 13, 2025 am 12:16 AM

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

カプセルの量を見つけるためのJavaプログラム カプセルの量を見つけるためのJavaプログラム Feb 07, 2025 am 11:37 AM

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

PHP:多くのウェブサイトの基礎 PHP:多くのウェブサイトの基礎 Apr 13, 2025 am 12:07 AM

PHPが多くのWebサイトよりも優先テクノロジースタックである理由には、その使いやすさ、強力なコミュニティサポート、広範な使用が含まれます。 1)初心者に適した学習と使用が簡単です。 2)巨大な開発者コミュニティと豊富なリソースを持っています。 3)WordPress、Drupal、その他のプラットフォームで広く使用されています。 4)Webサーバーとしっかりと統合して、開発の展開を簡素化します。

PHPの影響:Web開発など PHPの影響:Web開発など Apr 18, 2025 am 12:10 AM

phphassiblasifly-impactedwebdevevermentandsbeyondit.1)itpowersmajorplatformslikewordpratsandexcelsindatabase interactions.2)php'sadaptableability allowsitale forlargeapplicationsusingframeworkslikelavel.3)

See all articles