综合挑战Final Challenge
恭喜你学完了 Level 0 的所有知识!现在来做一个综合挑战,把学到的东西全用上。Congratulations on finishing all of Level 0! Now let's do a final challenge that uses everything you've learned.
写一个程序,在 Brain 屏幕上显示乘法表(1×1 到 5×5)。Write a program that displays a multiplication table (1×1 to 5×5) on the Brain screen.
8.1 要求8.1 Requirements
- 使用循环(for 或 while 都行)Use loops (for or while — either works)
- 每行显示一组乘法,格式像这样:
3 x 4 = 12Each line shows one multiplication, like this:3 x 4 = 12 - Brain 屏幕行数有限,显示满了要清屏重来The Brain screen has limited lines, so clear the screen when it's full
这次不给框架代码 — 请从空白的 main() 开始,自己写出来。This time there's no starter code — start from an empty main() and write it yourself.
别急着写代码!先在纸上理清思路:Don't rush into coding! Plan your approach on paper first:
- 乘法表有几行?每行显示什么?How many lines does the multiplication table have? What does each line show?
- 需要几层循环?外层控制什么?内层控制什么?How many nested loops do you need? What does the outer loop control? The inner loop?
- 怎么控制屏幕上每一行的位置?How do you control which line each result appears on?
8.2 参考思路8.2 Hints
如果你卡住了,可以一步步打开下面的提示。每次只打开一个,试试能不能继续。If you get stuck, open the hints below one at a time. Only open one at a time and try to continue on your own.
乘法表是一个"表格":第一个数从 1 到 5,第二个数也从 1 到 5。A multiplication table is a grid: the first number goes from 1 to 5, and the second number also goes from 1 to 5.
要遍历表格的每一格,需要两层嵌套循环:To go through every cell in the grid, you need two nested loops:
for (int a = 1; a <= 5; a++) {
for (int b = 1; b <= 5; b++) {
// a x b = ?
}
}
外层循环控制第一个数 a,内层控制第二个数 b。The outer loop controls the first number a, the inner loop controls the second number b.
Brain 屏幕可以用 setCursor(行, 列) 来指定文字显示的位置。行号从 1 开始。The Brain screen uses setCursor(row, column) to set where text appears. Row numbers start at 1.
你需要一个变量来记录"当前显示到第几行":You need a variable to track "which line we're currently on":
int row = 1;
// 每显示一行:
Brain.Screen.setCursor(row, 1);
Brain.Screen.print("...");
row++; // 下一行
Brain 屏幕大概能显示 10-12 行文字。你有 25 条乘法(5×5),所以需要中途清屏。The Brain screen can show about 10-12 lines of text. You have 25 multiplications (5×5), so you need to clear the screen midway.
可以判断行号是否超过屏幕能显示的行数:You can check if the row number exceeds what the screen can display:
if (row > 10) {
wait(2000, msec); // 让用户看 2 秒
Brain.Screen.clearScreen(); // 清屏
row = 1; // 重新从第 1 行开始
}
另外,每行之间加一点延迟(比如 wait(500, msec)),显示效果更好。Also, adding a small delay between lines (e.g. wait(500, msec)) makes it look better.
8.3 参考答案8.3 Reference Answer
自己写完后再看答案!对比一下你的写法和参考答案有什么不同。只要能正确显示乘法表,你的写法就是对的。Try writing it yourself before checking the answer! Compare your approach with the reference. As long as your multiplication table displays correctly, your code is correct.
8.4 拓展思考8.4 Extra Challenges
如果你做完了基础挑战,可以试试这些升级版:If you've completed the basic challenge, try these upgrades:
- 升级 1:改成 1×1 到 9×9 的完整乘法表Upgrade 1: Make it a full 1×1 to 9×9 multiplication table
- 升级 2:不重复 — 只显示
a ≤ b的情况(比如显示 2×3 但不显示 3×2)Upgrade 2: No duplicates — only show cases wherea ≤ b(e.g. show 2×3 but not 3×2) - 升级 3:用
if判断,如果结果是两位数以上就多空一格对齐Upgrade 3: Useifto add an extra space when the result has two or more digits, for alignment
8.5 动手预测8.5 Predict the Output
最后一道思考题:看下面这个嵌套循环,一共会打印几行?One last brain teaser: look at the nested loop below — how many lines will it print in total?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++) {
// 打印一行
}
}
这个嵌套循环一共打印几行?How many lines does this nested loop print in total?