课程E-循环2-嵌套循环和计分板

这节课介绍了一个复杂的循环和迭代的示例。

  • Computer science
  • looping
Print lesson
  • 年龄 9+
  • 65+ 分钟
  • JavaScript Blocks

介绍

这节课介绍了一个复杂的循环和迭代的示例。 建议你在尝试这节课程之前先把这个系列关于循环的第一节课“循环和动画”学完。 如果学生感到学习吃力,那么他们可能需要先回顾一下那节课。

当“循环和动画”只有一个循环时,这节课有一个嵌套的循环和一个简单的循环。

在这节课中,我们将尝试做一个计分板。我们的计分板会通过点亮LED灯来显示得分。因此,如果4颗LED灯被点亮,则得分是4。

老师们可能需要考虑这节关于嵌套循环课程的以下几点:

  1. 让学生们追踪外循环处于哪个数值或迭代中,以及内循环在做什么。 当老师运行代码的时候,他们可以用纸和铅笔或者按钮或者其他什么东西记录。 记住循环的起始值是0,因此这毫无疑问会让学生们在开始的时候感到困惑。
  2. 嵌套循环有很多种解释。 其中一个相关的解释是数学中的十位数。 如果你用这个数学例子来举例的话,那么十位数将是外循环,个位数将是内循环。 外循环的每个迭代将会有10个1。 如有需要,请参考这个例子
  3. 
    basic.forever(() => {
        for (let Tens = 0; Tens <= 9; Tens++) {
            for (let Ones = 0; Ones <= 9; Ones++) {
                if (Ones + 1 < 10) {
                    basic.showNumber(Ones + 1)
                }
            }
            basic.clearScreen()
            basic.pause(500)
            basic.showNumber(Tens + 1)
            basic.showString(" Tens")
        }
    })
          
  4. 试着也用现实生活中的例子来解释,例如:刷牙。 大多数人一天刷三次牙。 外循环可能是天数,内循环肯能是在某一天你刷牙的次数。 外循环可能会经历7次,内循环可能会经历3次。
  5. 这些挑战和问答题将引导学生找到一个方向的答案。 例如:一个问题可能会让你用右侧的LED灯来显示得分。 但是,如果你忽略了这一点,那么你肯能至少有两种方法在我们的计分板上显示正确的得分。 左侧或者顶部区域可能是显示得分的最显眼的地方。 你可能希望通过改变得分显示的位置来考考学生们,让他们寻找其他方法获得所需的分数。 这就是独立练习里面的要求,所以这样做可以帮助他们提前做好准备。 最终目标是让学生们体验这些循环是如何相互作用。 还有比这(对同一个问题给出多个答案)更好的方法吗?
  6. You may want to really get into the theme by showing pictures of things you will score. Make a real competition and have the students make their own scores for their competitors. Then they can invent a story to tell what happened in the contest or game they score.

我们的micro:bit课程是根据Code.org CS 基础课程知识应用而量身定做的。 在学生们开始这些课程之前,我们鼓励学生们首先完成所有的CS基础知识。 学生们应该熟悉Code.org CS基础中的循环。

背景知识

Students are highly encouraged to first complete the following:

The teacher is highly encouraged to first complete the following:

教学指南

打开

课堂活动

你将需要这些:

  • 1块micro:bit
  • 1根USB线
  • 1个AAA电池组(可选)
  • 2节AAA电池(可选)

编写文本

for (let index = 0; index <= 4; index++) {}

for

按规定次数运行部分程序。

basic.forever(() => {})

forever

在后台保持运行一部分程序。


让 项目= 0
项目 = 0
            

设置项目

用一个等号让一个变量存储数字或你指定的字符串。

led.plot(0, 0)

+ 绘制

Turn on the LED light you say on the LED screen.

4个步骤

指导练习

我们将在网上提供micro:bit教程或代码示例。接下来,去到https://makecode.microbit.org

let Column = 0
let Row = 0
Row = 1
Column = 4
basic.forever(() => {
    for (let RowCounter = 0; RowCounter <= Row; RowCounter++) {
        for (let ColumnCounter = 0; ColumnCounter <= Column; ColumnCounter++) {
            led.plot(ColumnCounter, RowCounter)
        }
    }
})

This code has two loops.

The two loops show nested loops. That means there is a loop and inside that is another loop.

The outer loop represents the row of LEDs. In its current state, the code will go through 2 times.(0-1)

Finally, there is the inner loop represents the column of LEDs. In its current state, the code will light up 5 LEDs in each row.(0-4)

Our task is to score something. You choose the theme and what we score! It could be any contest and picking a theme that your students like will most certainly make the activity more fun. Some ideas are a surf competition, baseball game, dog contest, or even a baking contest. The micro:bit will be the scoreboard. Each score will be shown by the number of LEDs that are lit up.

The finished code is available here.

挑战

挑战1

Change the score to 9 points. Try to show your score as close to the left side of the micro:bit as you can.

查看代码

let Column = 0
let Row = 0
Row = 2
Column = 2
basic.forever(() => {
    for (let RowCounter = 0; RowCounter <= Row; RowCounter++) {
        for (let ColumnCounter = 0; ColumnCounter <= Column; ColumnCounter++) {
            led.plot(ColumnCounter, RowCounter)
        }
    }
})
          

The finished code is available here.

挑战2

Change the score to 15 points. Try to show your score as close to the left of the micro:bit as you can.

查看代码

let Column = 0
let Row = 0
Row = 4
Column = 2
basic.forever(() => {
    for (let RowCounter = 0; RowCounter <= Row; RowCounter++) {
        for (let ColumnCounter = 0; ColumnCounter <= Column; ColumnCounter++) {
            led.plot(ColumnCounter, RowCounter)
        }
    }
})
          

The finished code is available here.

挑战3

Change the score to 16 points.

查看代码

let Column = 0
let Row = 0
Row = 3
Column = 3
basic.forever(() => {
    for (let RowCounter = 0; RowCounter <= Row; RowCounter++) {
        for (let ColumnCounter = 0; ColumnCounter <= Column; ColumnCounter++) {
            led.plot(ColumnCounter, RowCounter)
        }
    }
})
          

The finished code is available here.

挑战4

Show a perfect score of 25! All LEDs should be lit up! Try to show your score as close to the left of the micro:bit as you can.

查看代码

let Column = 0
let Row = 0
Row = 4
Column = 4
basic.forever(() => {
    for (let RowCounter = 0; RowCounter <= Row; RowCounter++) {
        for (let ColumnCounter = 0; ColumnCounter <= Column; ColumnCounter++) {
            led.plot(ColumnCounter, RowCounter)
        }
    }
})
          

The finished code is available here.

独立练习

Choose a score and tell your teacher your goal before beginning. Try to show this number in two different ways. You might need to write down your solutions and then after you find both ways, call the teacher over to show off your work. Make it a contest between two things or people and create a story to justify the score.

问答

问题 1

What is a real life situation that fits a nested loop?

显示答案

Answers will vary. One example is a song. Verses are the outer loop and Chorus is the inner loop. Sing the verse then chorus... repeat.

问题 2

Is it easy to show an odd number?

显示答案

Yes. There are several ways. One might be to just show the first row or just the first column. That would be 5.

问题 3

Why are loops used?

显示答案

Loops are used to repeat code in one specific spot of your program