boolean逻辑
boolean逻辑运算有两个结果:真(yes)或假(no)
例如:
1 > 0 结果为真(yes)
1 < 0 结果为假(no)
- 在and指令中
当两个条件都为真(yes)时,结果为真(yes);
只要有一个条件为假(no),结果为假(no)。 - 在or指令中
只要有一个条件为真(yes),结果为真(no);
两个条件都为假(no)时,结果为假(no)。 - 在no指令中
条件为真(yes)时,结果为假(no);
条件为假(no)时,结果为真(yes)。
注意:实际编程中,我们用true来表示yes,false表示no。
and指令(与)
(and会让小猴等到前后两个条件都成立的时候再行动)
and两边的两个条件都成立,判断条件才成立。
相关题目:128~133
//128
until bear.sleeping() and tiger.sleeping()
wait()
step 20
//129
until bear.sleeping() and tiger.sleeping()
wait()
step 20
//130
//#修复这个条件:
until bear.sleeping() and tiger.sleeping()//#还需要等谁睡着?
wait()
for b in bananas
goto b
//131
for b in bananas
//#修复这个条件:
until bear.sleeping() and tiger.sleeping()//#还需要等谁睡着?
wait()
if b.green()
goat.goto b
else
monkey.goto b
//132
waitForSafety = () ->
//#在这里使用 and 指令:
until tiger.sleeping() and bear.sleeping()
wait()
for b in bananas
waitForSafety()
goto b
//133
for b in bananas
//#这里需要等待白虎和大熊都睡着……
until bear.sleeping() and tiger.sleeping()
# Your code here
wait()
if b.frozen()
goat.goto b
goat.hit()
//#这里也一样需要等待!
until bear.sleeping() and tiger.sleeping()
# Your code here
wait()
goto b
or指令(或)
or两边的条件只要有一个成立,那么判断条件就成立。
相关题目:134~140
//134(注意体会or的用法)
until tiger.sleeping() or tiger.playing()
wait()
step 20
//135
until tiger.sleeping() or tiger.playing()
wait()
goto banana
//136
until tiger.sleeping() or tiger.playing()
wait()
step 10
//#这里要等到大熊睡着或者玩耍
until bear.sleeping() or bear.playing()
wait()
step 10
//137
//#修复这段代码:
until bear.sleeping() or bear.playing()
wait()
goat.goto banana
//138
for b in bananas
//#这里需要等待一下
until tiger.sleeping() or tiger.playing()
# Your code here
wait()
if b.green()
goat.goto b
else
goto b
//139
for b in bears
//#这里需要等待<b>大熊</b>睡着或者开始玩耍
until b.sleeping() or b.playing()
# Your code here
wait()
step 10
//140
waitFor = (t) ->
//#完成这里的代码吧!
until t.sleeping() or t.playing()
# Your code here
wait()
for t in tigers
for stepper in [monkey, goat]
waitFor t
stepper.step 10
//(尝试内层循环和外层循环换一下,体会两层循环)
waitFor = (t) ->
//#完成这里的代码吧!
until t.sleeping() or t.playing()
# Your code here
wait()
for stepper in [monkey, goat]
for t in tigers
waitFor t
stepper.step 10
and和or用法总结
and需要两边的条件都成立,结果才为真;
or只需要满足一个条件,结果就为真。
相关题目:141~145
//141
for p in penguins
if p.hasGlasses()
p.step 15
if p.hasBowTie()
p.step 20
//#企鹅们组成了一座桥~
//#现在小猴可以去拿香蕉啦!
goto banana
//142
//#只移动戴眼镜且打领结的企鹅。
for p in penguins
if p.hasGlasses() and p.hasBowTie()
p.step 16
goto banana
//143
//#指挥戴眼镜的企鹅移动。
//#打领结的企鹅移动9步。
//#其他所有企鹅移动14步。
for p in penguins
if p.hasBowTie() and p.hasGlasses()
# Your code here
p.step 9
else
# Your code here
p.step 14
goto banana
//144
for p in penguins
//#好好检查下企鹅是戴眼镜的还是打领结的。
if p.hasGlasses() or p.hasBowTie()
p.step 10
goto banana
//145
//#这个for循环会首先让山羊执行指令,
//#然后才到小猴
for stepper in [goat, monkey]
for p in penguins
# Your code here
if p.hasGlasses() or p.hasBowTie()
# Your code here
p.step 10
stepper.step 20
not指令
not指令把原先是yes的结果变为no,把原来为no的结果变为yes
相关题目:146~151
//146
if not banana.green()
//#你只能修改这里的代码:
goto banana
//147
for b in bananas
turnTo b
if b.rotten()
say "Yuck!"
else
goto b
//148
goto bush
goto bananas[0]
//149
if not banana.rotten()
goto banana
//150
for b in bananas
//#修复这里的代码:
if not b.rotten()
goto b
//151
for b in bananas
# Your code here
if not b.rotten()
# Your code here
goto b
else
goto bush