# 课程 F——条件语句 1——条件与天气预测

• 年龄 10+
• 65+ 分钟
• MakeCode Editor

## 课堂活动

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

### 编写文本

``````if (true) {

} else if (false) {

} else {

}``````

### if

``````

``````

### 设置项目

``let item = Math.randomRange(0, 10)``

### 随机

``basic.showIcon(IconNames.Heart)``

### 显示图标

``basic.showString("Hello!")``

### 显示字符串

``input.onButtonPressed(Button.A, () => {})``

4个步骤

## 指导练习

``````
let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
Random_Weather = Math.randomRange(0, 2)
if (Random_Weather == 0) {
basic.showString("Raining")
} else {
basic.showString("Play Outside!")
}
SmoothOut_Animation()
})
function SmoothOut_Animation()  {
basic.pause(1000)
basic.clearScreen()
}
``````

Micro:bit可以预测天气了哦！

## 挑战

### 挑战1

``````
let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
Random_Weather = Math.randomRange(0, 2)
if (Random_Weather == 0) {
basic.showString("Raining")
basic.showIcon(IconNames.Umbrella)
} else {
basic.showString("Play Outside!")
basic.showIcon(IconNames.Rollerskate)
}
SmoothOut_Animation()
})
function SmoothOut_Animation()  {
basic.pause(1000)
basic.clearScreen()
}
``````

### 挑战2

``````
let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
Random_Weather = Math.randomRange(0, 2)
if (Random_Weather == 0) {
basic.showString("Sunny / Warm")
basic.showIcon(IconNames.TShirt)
} else {
basic.showString("Play Outside!")
basic.showIcon(IconNames.Rollerskate)
}
SmoothOut_Animation()
})
function SmoothOut_Animation()  {
basic.pause(1000)
basic.clearScreen()
}
``````

### 挑战3

``````
let Random_Weather = 0
input.onButtonPressed(Button.A, () => {
Random_Weather = Math.randomRange(0, 2)
if (Random_Weather == 0) {
basic.showString("Snowy / Cold")
basic.showIcon(IconNames.Chessboard)
} else {
basic.showString("Play Outside!")
basic.showIcon(IconNames.Rollerskate)
}
SmoothOut_Animation()
})
function SmoothOut_Animation()  {
basic.pause(1000)
basic.clearScreen()
}
``````

## 问答

### 问题 2

``````
Set DONUTS to 12

if DONUTS > 20 then
show string "SHARE"
else
show string "EAT ALONE"
``````

### 问题 3

``````
If I_AM_HUNGRY THEN
else
``````

``````
If I_AM_HUNGRY THEN
show string "I eat food"
else
show string "I don't eat"
``````