比较
比较也就是编程中的关系运算,主要有以下几种:
- 等于 ==
- 小于 <
- 大于 >
- 小于等于 <=
- 大于等于 >=
==:判断是否相等的指令,可以用来确认两个数字的数值是否相等。
相关题目:154~157
health()函数返回猴子的健康值,可以用上面的比较指令对这个值进行判断
例如:
判断猴子健康值是否是100: health() == 100
判断猴子健康值是否大于60: health() > 60
判断猴子健康值是否小于或等于40:health() <= 40
//154
if health() == 40 //#40不等于100,所以判断结果是no
goto banana
//155
goto healthZone
//#修改这里,健康度永远到不到100。
until health() == 100
wait()
goto banana
//156
for b in bananas
goto healthZone
//#在这里等待,直到小猴的健康度恢复到100。
until health() == 100
//# Your code here
wait()
goto b
//157
goto healthZone
until health() == 100
# Your code here
wait()
goto banana
小于号<:可以用来比较一个数值是否小于另外一个数值。
相关题目:158~160
//158
if health() < 90 //#80小于90,所以判断结果是yes
goto banana
//159
for b in bananas
goto b
//#提示: 如果健康度低于70,就需要去休息区恢复啦
if health() < 70
//# Your code here
goto healthZone
until health() == 100
//# Your code here
wait()
//160
//#这个函数是有用的:
gotoNearestHealth = () ->
d0 = distanceTo healthZones[0]
d1 = distanceTo healthZones[1]
if d0 < d1
goto healthZones[0]
else
goto healthZones[1]
for b in bananas
goto b
if health() < 60
gotoNearestHealth()
//#在这里等待,直到小猴恢复健康!
until health() == 100
//# Your code here
wait()
return
return可以把函数的运算结果返回
相关题目:161
//161
example = () ->
return not banana.rotten()
if example() //#example()返回的值是no
goto banana
else
turn 360
在这一关中,example()可以判断香蕉是否已经腐烂了:
如果香蕉腐烂,那么banana.rotten()是yes,not banana.rotten()是no,从而example()的结果就是no;
相反如果香蕉没有腐烂,那么banana.rotten()是no,not banana.rotten()是yes,从而example()的结果就是yes.
函数参数
参数是定义函数时括号中的内容,在函数中表示任何一个物体。
相关题目:162
yummy = (x) ->
return not x.rotten()
//#修复这里吧:
for b in bananas
//#这里调用了yummy函数来判断 b 是不是美味的
if yummy b
goto b
代码首先定义了一个函数yummy:
- yummy = (x) ->:yummy是函数名,之后可以通过它来使用函数;x是参数,它可以表示任何物体,在使用函数时用具体的物体来取代它;=和->是函数定义的标志;
- return not x.rotten():函数的内容,表示这个函数的功能是返回物体x是否腐烂了,如果物体x腐烂,返回no;如果物体x未腐烂,返回yes;
下面是正式的代码:
- 对于每个香蕉,先使用yummy函数"yummy b",用b代替参数x,“yummy b”判断香蕉b是否腐烂,根据yummy函数的返回结果决定是否"goto b"。
- 如果香蕉b腐烂,返回结果是no,不执行"goto b";如果香蕉b不腐烂,返回结果yes,执行"goto b"
163~165题
//163
healthy = () ->
return health() == 100
goto healthZone
//#在这里使用healthy()函数
until healthy()
//# Your code here
wait()
goto banana
//164
healthy = () ->
return health() == 100
injured = () ->
return health() <= 80
//#在这里使用healthy()和injured()两个条件进行判断:
for b in bananas
//# Your code here
if injured()
//# Your code here
goto healthZone
until healthy()
//# Your code here
wait()
goto b
//165
healthy = () ->
//#修复这里吧
return health() > 80
//#如果你已经修复了 healthy() 函数,这里就应该是正常的。
goto healthZone
until healthy()
wait()
goto bush
goto banana