国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
Java のパターンの例
例 1: 數(shù)字を使用してピラミッドの半分を印刷します。
例 2: 數(shù)字の矢印を印刷します。
例 3: 星(*) を使用して完全なピラミッドを印刷します。
例 4: 數(shù)字を使用して半逆ピラミッドを印刷します。
例 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 のプログラミング言語(yǔ)を?qū)W習(xí)し、高度な概念に深く入る前に、ループの仕組みを理解することが重要です。ループには for、while、do-while ループの 3 種類があります。各ループは互いにわずかに異なるため、プログラムの特定の狀況に応じて使用されます。さまざまなループを使用するには、何らかのプログラミング ロジックが必要であり、そのために論理的思考力を必要とするパターン練習(xí)がプログラマーに與えられます。たとえば、幾何學(xué)的図形 (三角形、四角形など)、ピラミッド、星、數(shù)字、文字スタイルのさまざまなパターンのボックスをコンソール畫面に印刷できます。ループの形式や基本構(gòu)文はプログラミング言語(yǔ)によって異なる場(chǎng)合がありますが、これらのパターンを出力する一般的なロジックは同じです。

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

Java のパターンの例

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

例 1: 數(shù)字を使用してピラミッドの半分を印刷します。

コード:

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 ループは行數(shù)を指定します。この場(chǎng)合、行 (つまり 5 つ) を定義しました。それ以外の場(chǎng)合は、ユーザーからの入力を取得して変數(shù)に格納することもできます。內(nèi)側(cè)のループは、特定の行の數(shù)値を出力します。 1 行の完了または ‘j’ ループの終了後、println() を使用して行が変更されます。

例 2: 數(shù)字の矢印を印刷します。

コード:

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 つのループを使用する必要があります。行の前半は行に設(shè)定された初期値になりますが、行數(shù)は下半分の初期値より 1 減ります。両方の半分の內(nèi)側(cè)のループは、外側(cè)のループに従って各行を反復(fù)処理するために使用されます。

例 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 変數(shù)まで動(dòng)作するピラミッドの出力の合計(jì)行數(shù)を念頭に置きます。次に、最初にピラミッド內(nèi)のスペースを印刷し、次にスペースの後にパターン (*) を印刷する必要があります。この 2 番目と 3 番目では、外側(cè)のループ「i」內(nèi)で for ループが使用されます。

例 4: 數(shù)字を使用して半逆ピラミッドを印刷します。

コード:

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 のパターン

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

例 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 つは行數(shù)用、もう 1 つは特定の行の文字出力用です。ただし、最も注意しなければならないのは文字データの扱いです。たとえば、Java では「A」の數(shù)値は 65 なので、すべての數(shù)學(xué)的論理はアルファベットの數(shù)値を使用して実行され、最終的には文字形式で出力されます。

例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 と似ていますが、唯一の違いは、目的のパターンを出力するために使用される?yún)g純なロジックです。

例 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 のパターンの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場(chǎng)合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫像を無(wú)料で

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

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

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無(wú)料のコードエディター

SublimeText3 中國(guó)語(yǔ)版

SublimeText3 中國(guó)語(yǔ)版

中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

Javaのマップを反復(fù)する方法は? Javaのマップを反復(fù)する方法は? Jul 13, 2025 am 02:54 AM

Javaにはマップを通過する3つの一般的な方法があります。1。エントリセットを使用してキーと値を同時(shí)に取得します。これは、ほとんどのシナリオに適しています。 2。キーセットまたは値をそれぞれキーまたは値を通過する。 3. Java8のForeachを使用して、コード構(gòu)造を簡(jiǎn)素化します。 EntrySetは、すべてのキー値ペアを含むセットを返し、各ループはキーと値に頻繁にアクセスするのに適したMap.entryオブジェクトを取得します。キーまたは値のみが必要な場(chǎng)合は、それぞれkeyset()またはvalues()を呼び出すことができます。または、キーを橫斷するときにmap.get(key)を介して値を取得できます。 Java 8はForeachを使用できます((key、value) - &gt

Javaオプションの例 Javaオプションの例 Jul 12, 2025 am 02:55 AM

オプションは、意図を明確に表現(xiàn)し、ヌルの判斷のコードノイズを減らすことができます。 1. optional.ofnullableは、nullオブジェクトに対処する一般的な方法です。たとえば、マップから値を取得する場(chǎng)合、Orelseを使用してデフォルト値を提供できるため、ロジックはより明確かつ簡(jiǎn)潔になります。 2.チェーンコールマップを使用してネストされた値を達(dá)成してNPEを安全に回避し、リンクが無(wú)効である場(chǎng)合はデフォルト値を返す場(chǎng)合は自動(dòng)的に終了します。 3.フィルターは條件付きフィルタリングに使用でき、その後の操作は條件が満たされた場(chǎng)合にのみ実行され続けます。そうしないと、軽量のビジネス判斷に適したOrelseに直接ジャンプします。 4.基本的なタイプや単純なロジックなど、複雑さを高めるなど、オプションを過剰使用することはお?jiǎng)幛幛筏蓼护?。一部のシナリオはNUに直接戻ります。

java.io.notserializableExceptionを修正する方法は? java.io.notserializableExceptionを修正する方法は? Jul 12, 2025 am 03:07 AM

java.io.notserializableExceptionに遭遇するためのコアワークアウンドは、シリアル化する必要があるすべてのクラスがシリアル化可能なインターフェイスを?qū)g裝し、ネストされたオブジェクトのシリアル化サポートを確認(rèn)することです。 1.メインクラスに機(jī)器を追加する可能性のあるものを追加します。 2.クラス內(nèi)の対応するカスタムフィールドのクラスも、シリアル化可能なものを?qū)g裝していることを確認(rèn)します。 3.一時(shí)的に使用して、シリアル化する必要のないフィールドをマークする。 4.コレクションまたはネストされたオブジェクトの非シリアル化されたタイプを確認(rèn)します。 5.どのクラスがインターフェイスを?qū)g裝していないかを確認(rèn)します。 6.キーデータの保存やシリアル化可能な中間構(gòu)造の使用など、変更できないクラスの交換設(shè)計(jì)を検討します。 7.変更を検討してください

Javaの比較対Comparator Javaの比較対Comparator Jul 13, 2025 am 02:31 AM

Javaでは、Defaultのデフォルトソートルールを內(nèi)部的に定義するためにAcparableが使用され、コンパレータを使用して複數(shù)のソートロジックを外部から定義します。 1.Comparableは、クラス自體によって実裝されるインターフェイスです。比較()メソッドを書き換えることにより、自然な順序を定義します。弦や整數(shù)など、固定および最も一般的に使用されるソートメソッドを備えたクラスに適しています。 2。Comparatorは、同じクラスに複數(shù)の並べ替え方法が必要な狀況に適した、Compare()メソッドを介して実裝された外部定義の機(jī)能インターフェイスであり、クラスソースコードを変更できない、またはソートロジックが変更されることが多い場(chǎng)合があります。 2つの違いは、比較可能がソートロジックを定義するだけで、クラス自體を変更する必要があることですが、比較して

Javaメソッドリファレンスが説明されています Javaメソッドリファレンスが説明されています Jul 12, 2025 am 02:59 AM

メソッドリファレンスは、JavaでのLambda式の執(zhí)筆を簡(jiǎn)素化する方法であり、コードをより簡(jiǎn)潔にします。これは新しい構(gòu)文ではなく、機(jī)能的インターフェイスのコンテキストに適したJava 8によって導(dǎo)入されたLambda式への近道です。コアは、既存のメソッドを関數(shù)インターフェイスの実裝として直接使用することです。たとえば、system.out :: printlnはs-> system.out.println(s)に相當(dāng)します。メソッド參照には4つの主要な形式があります。1。靜的メソッドリファレンス(className :: staticMethodName); 2。インスタンスメソッド參照(特定のオブジェクトへのバインディング、Instance :: MethodName); 3。

Javaのキャラクターエンコーディングの問題を処理する方法は? Javaのキャラクターエンコーディングの問題を処理する方法は? Jul 13, 2025 am 02:46 AM

Javaのキャラクターエンコーディングの問題に対処するために、重要なのは、各ステップで使用されるエンコードを明確に指定することです。 1.テキストを読み書きするときは常にエンコードを指定し、inputstreamreaderとoutputStreamWriterを使用し、明示的な文字セットを渡して、システムのデフォルトエンコードに依存しないようにします。 2.ネットワーク境界で文字列を処理するときに両端が一貫していることを確認(rèn)し、正しいコンテンツタイプのヘッダーを設(shè)定し、ライブラリでエンコードを明示的に指定します。 3. string.getBytes()およびNewString(byte [])を注意して使用し、プラットフォームの違いによって引き起こされるデータの破損を避けるために、常に手動(dòng)でstardantcharsets.utf_8を指定します。要するに、

JavaでJsonを解析する方法は? JavaでJsonを解析する方法は? Jul 11, 2025 am 02:18 AM

JavaでJSONを解析するには、Jackson、GSON、またはorg.jsonを使用する3つの一般的な方法があります。 1.ジャクソンは、パフォーマンスと包括的な機(jī)能を備えたほとんどのプロジェクトに適しており、オブジェクトとJSON文字列間の変換と注釈マッピングをサポートしています。 2。GSONは、Androidプロジェクトや軽量のニーズにより適しており、使いやすいですが、複雑な構(gòu)造と高性能シナリオの処理がわずかに劣っています。 3.org.jsonは、単純なタスクや小さなスクリプトに適しており、柔軟性とタイプの安全性がないため、大規(guī)模なプロジェクトにはお?jiǎng)幛幛筏蓼护?。選択は、実際のニーズに基づいて決定されるべきです。

新しい電子メールのOutlookショートカット 新しい電子メールのOutlookショートカット Jul 11, 2025 am 03:25 AM

Outlookで新しい電子メールを迅速に作成する方法は次のとおりです。1。デスクトップバージョンでは、ショートカットキーCtrl Shift Mを使用して、新しい電子メールウィンドウを直接ポップアップします。 2。Webバージョンは、JavaScript(javaScript:document.querySelector( "divrole = 'button'" "など)を含むブックマークを作成することにより、ワンクリックで新しい電子メールを作成できます。 3.ブラウザプラグイン(Vimium、CrxMousegesturesなど)を使用して、「新しいメール」ボタンをトリガーします。 4. Windowsユーザーは、タスクバーのOutlookアイコンを右クリックすることで「新しいメール」を選択することもできます

See all articles