图案练习与小挑战
练习嵌套循环图案:空心矩形、居中金字塔和对角线。逐步完成小挑战。
图案练习与小挑战 是 CoddyKit 上的免费 Java Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 3 节课。
模式技巧
文本模式技巧:
- 外层循环控制行
- 内层循环构建每一行
- 将字符追加到 String 中,然后只打印一次
- 使用条件选择星号或空格
空心矩形
空心矩形 3x5
public class Main {
public static void main(String[] args) {
// Outer loop: goes through each row (1 to 3)
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
// Inner loop: goes through each column (1 to 5)
for (int c = 1; c <= 5; c = c + 1) {
// Print a star if we are on:
// - the first row (r == 1)
// - the last row (r == 3)
// - the first column (c == 1)
// - the last column (c == 5)
if (r == 1 || r == 3 || c == 1 || c == 5) {
line = line + "*";
} else {
// Otherwise, print a space (inside of the rectangle)
line = line + " ";
}
}
// Print the completed row
System.out.println(line);
}
}
}
居中金字塔
居中金字塔,高度为 4
public class Main {
public static void main(String[] args) {
int h = 4; // height of the pyramid
// Outer loop: goes through each row (1 to h)
for (int r = 1; r <= h; r = r + 1) {
String line = "";
// First inner loop: add spaces before the stars
// (h - r) spaces so the stars are centered
for (int s = 1; s <= (h - r); s = s + 1) {
line = line + " ";
}
// Second inner loop: add stars
// Formula (2*r - 1) makes the row width odd (1, 3, 5, 7, ...)
for (int c = 1; c <= (2 * r - 1); c = c + 1) {
line = line + "*";
}
// Print the completed row
System.out.println(line);
}
}
}
对角线
4x4 正方形中的对角线
public class Main {
public static void main(String[] args) {
int n = 4; // size of the square (4 rows and 4 columns)
// Outer loop: controls rows (1 to n)
for (int r = 1; r <= n; r = r + 1) {
String line = "";
// Inner loop: controls columns (1 to n)
for (int c = 1; c <= n; c = c + 1) {
// If row index == column index, print a star
if (r == c) {
line = line + "*";
} else {
// Otherwise, print a space
line = line + " ";
}
}
// Print the row
System.out.println(line);
}
}
}
小挑战
小挑战
- 高度为 4 的空心直角三角形
- 使用两个字符构建 4x4 棋盘格
- 高度为 5 的居中菱形
将步骤写得简短,构建行字符串,然后打印。
模式演示
运行它:打印三种模式,不需要任何控制台输入。
public class Main {
public static void main(String[] args) {
// 1) Hollow rectangle 3x5
for (int r = 1; r <= 3; r = r + 1) {
String line = "";
for (int c = 1; c <= 5; c = c + 1) {
if (r == 1 || r == 3 || c == 1 || c == 5) line = line + "*";
else line = line + " ";
}
System.out.println(line);
}
// 2) Centered pyramid height 4
int h = 4;
for (int r = 1; r <= h; r = r + 1) {
String line = "";
for (int s = 1; s <= (h - r); s = s + 1) line = line + " ";
for (int c = 1; c <= (2 * r - 1); c = c + 1) line = line + "*";
System.out.println(line);
}
// 3) Diagonal in a 4x4 square
int n = 4;
for (int r = 1; r <= n; r = r + 1) {
String line = "";
for (int c = 1; c <= n; c = c + 1) {
if (r == c) line = line + "*";
else line = line + " ";
}
System.out.println(line);
}
}
}
空心矩形检查
快速检查:哪些代码片段会打印 3x5 空心矩形?
回顾与下一步
回顾:您练习了空心图形、居中金字塔和对角线。继续使用嵌套循环构建模式。
下一步:学习数组,以存储和处理集合。
常见问题解答
「图案练习与小挑战」课时是免费的吗?
是的 — 「图案练习与小挑战」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。
「图案练习与小挑战」这节课中我会学到什么?
练习嵌套循环图案:空心矩形、居中金字塔和对角线。逐步完成小挑战。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Java Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「图案练习与小挑战」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Java Academy 课中编写并运行代码吗?
能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- for 循环基础
- 嵌套 for 循环与文本图案
- 图案练习与小挑战