ネストしたforとテキストパターン
ネストしたforループを使って、長方形、直角三角形、小さな表を出力します。外側のループで行、内側のループで列を制御します。
「ネストしたforとテキストパターン」はCoddyKit上の無料Java Academyレッスンです。 これはレッスン2/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJava Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Java Academyコースには全3レッスンが含まれています。
入れ子の概念
入れ子のループ: 別のループの中にあるループです。多くの場合、外側のループで行を制御し、内側のループで列を制御します。
長方形 3x4
長方形 3x4
public class Main {
public static void main(String[] args) {
// Outer loop controls the number of rows (3 rows total)
for (int r = 1; r <= 3; r = r + 1) {
// Start with an empty string for each row
String line = "";
// Inner loop controls the number of columns (4 stars per row)
for (int c = 1; c <= 4; c = c + 1) {
// Add one star to the line in each iteration
line = line + "*";
}
// Print the completed line of stars
System.out.println(line);
}
}
}
直角三角形
直角三角形
public class Main {
public static void main(String[] args) {
// Outer loop controls the number of rows (from 1 up to 4)
for (int r = 1; r <= 4; r = r + 1) {
// Start with an empty string for each row
String line = "";
// Inner loop runs 'r' times, so stars increase with each row
for (int c = 1; c <= r; c = c + 1) {
// Add one star to the current line
line = line + "*";
}
// Print the completed line of stars
System.out.println(line);
}
}
}
右寄せ
右寄せの三角形: 星印の前に空白を追加します。
public class Main {
public static void main(String[] args) {
int height = 4; // total number of rows for the triangle
// Outer loop: controls the row number
for (int r = 1; r <= height; r = r + 1) {
String line = "";
// First inner loop: add spaces so stars are right-aligned
for (int s = 1; s <= (height - r); s = s + 1) {
line = line + " ";
}
// Second inner loop: add stars according to current row
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
// Print the final line (spaces + stars)
System.out.println(line);
}
}
}
1..3 の表
表 1..3
public class Main {
public static void main(String[] args) {
// Outer loop controls the rows (1 to 3)
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
// Inner loop controls the columns (1 to 3)
for (int c = 1; c <= 3; c = c + 1) {
// Multiply row number (r) by column number (c)
// Add the result plus a space to the line
line = line + (r * c) + " ";
}
// Print the row after building it
System.out.println(line);
}
}
}
パターンのデモ
実行してみましょう: コンソール入力なしで3つのパターンを出力します。
public class Main {
public static void main(String[] args) {
// Rectangle 3x4
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
for (int c = 1; c <= 4; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
// Right triangle
for (int r = 1; r <= 4; r = r + 1) {
String line = "";
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
// Right-aligned triangle
int height = 4;
for (int r = 1; r <= height; r = r + 1) {
String line = "";
for (int s = 1; s <= (height - r); s = s + 1) {
line = line + " ";
}
for (int c = 1; c <= r; c = c + 1) {
line = line + "*";
}
System.out.println(line);
}
}
}
3x4 の長方形
クイックチェック: *で3x4の長方形(3行、4列)を出力するスニペットはどれですか?
まとめと次のステップ
まとめ: 入れ子のループを使って、長方形、三角形、空白による位置合わせ、小さな表を作成しました。
次のステップ: パターンに慣れるための練習とミニチャレンジに取り組みます。
よくある質問
「ネストしたforとテキストパターン」レッスンは無料ですか?
はい。「ネストしたforとテキストパターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Java Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Java Academyコースには全3レッスンが含まれています。
「ネストしたforとテキストパターン」で何を学びますか?
ネストしたforループを使って、長方形、直角三角形、小さな表を出力します。外側のループで行、内側のループで列を制御します。 ブラウザで直接実行するハンズオンコードでJava Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Java Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJava Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/3です。
「ネストしたforとテキストパターン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJava Academyレッスンでコードを書いて実行できますか?
はい。すべてのJava Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- forループの基礎
- ネストしたforとテキストパターン
- パターン練習とミニチャレンジ