CodeMonkey题解之154~165(比较和函数)

比较

比较也就是编程中的关系运算,主要有以下几种:

  • 等于 ==
  • 小于 <
  • 大于 >
  • 小于等于 <=
  • 大于等于 >=

==:判断是否相等的指令,可以用来确认两个数字的数值是否相等。

相关题目: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值