0Pricing
Java Academy · 课时

嵌套 for 循环与文本图案

使用嵌套 for 循环打印矩形、直角三角形和简单表格。控制行(外层循环)与列(内层循环)。

嵌套 for 循环与文本图案 是 CoddyKit 上的免费 Java Academy 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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);
    }
  }
}

模式演示

运行它:打印三种模式,不需要任何控制台输入。

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 循环与文本图案」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。

「嵌套 for 循环与文本图案」这节课中我会学到什么?

使用嵌套 for 循环打印矩形、直角三角形和简单表格。控制行(外层循环)与列(内层循环)。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Java Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。

「嵌套 for 循环与文本图案」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Java Academy 课中编写并运行代码吗?

能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. for 循环基础
  2. 嵌套 for 循环与文本图案
  3. 图案练习与小挑战
← 返回 Java Academy