这本书的上半部分,你输出了一些东西,并且调用了函数,不过一切都是直线式进行的。你的脚本从最上面一行开始,一路运行到结束,但其中并没有确定程序流向的分支点。现在你已经学会了 `if`、`else`和 `elsif`,你就可以开始建立包含条件判断的脚本了。
上一个脚本中你写了一系列的简单提问测试。这节的脚本中,你将需要向使用者提问,依据使用者的答案来做出决定。把脚本写下来,多多捣鼓一阵子,看看他的运作原理是什么。
~~~
def prompt
print "> "
end
puts "You enter a dark room with two doors. Do you go through door #1 or door #2?"
prompt; door = gets.chomp
if door == "1"
puts "There's a giant bear here eating a cheese cake. What do you do?"
puts "1\. Take the cake."
puts "2\. Scream at the bear."
prompt; bear = gets.chomp
if bear == "1"
puts "The bear eats your face off. Good job!"
elsif bear == "2"
puts "The bear eats your legs off. Good job!"
else
puts "Well, doing #{bear} is probably better. Bear runs away."
end
elsif door == "2"
puts "You stare into the endless abyss at Cthuhlu's retina."
puts "1\. Blueberries."
puts "2\. Yellow jacket clothespins."
puts "3\. Understanding revolvers yelling melodies."
prompt; insanity = gets.chomp
if insanity == "1" or insanity == "2"
puts "Your body survives powered by a mind of jello. Good job!"
else
puts "The insanity rots your eyes into a pool of muck. Good job!"
end
else
puts "You stumble around and fall on a knife and die. Good job!"
end
~~~
这里的重点是你可以在 `if` 语句中内部再放一个 `if` 语句。这是一个很强大的功能,可以用来建立「巢状(nested)」的决定(decision)。
你需要理解` if `语句包含 `if` 语句的概念。做一下加分习题,这样你会确信自己真正理解了它们。
# 你应该看到的结果
* * * * *
我在玩一个小冒险游戏。我的水准不怎么样。
~~~
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There's a giant bear here eating a cheese cake. What do you do?
1\. Take the cake.
2\. Scream at the bear.
> 2
The bear eats your legs off. Good job!
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There's a giant bear here eating a cheese cake. What do you do?
1\. Take the cake.
2\. Scream at the bear.
> 1
The bear eats your face off. Good job!
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthuhlu's retina.
1\. Blueberries.
2\. Yellow jacket clothespins.
3\. Understanding revolvers yelling melodies.
> 1
Your body survives powered by a mind of jello. Good job!
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 2
You stare into the endless abyss at Cthuhlu's retina.
1\. Blueberries.
2\. Yellow jacket clothespins.
3\. Understanding revolvers yelling melodies.
> 3
The insanity rots your eyes into a pool of muck. Good job!
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> stuff
You stumble around and fall on a knife and die. Good job!
$ ruby ex31.rb
You enter a dark room with two doors. Do you go through door #1 or door #2?
> 1
There's a giant bear here eating a cheese cake. What do you do?
1\. Take the cake.
2\. Scream at the bear.
> apples
Well, doing apples is probably better. Bear runs away.
~~~
# 加分习题
* * * * *
为游戏添加新的部分,改变玩家做决定的位置。尽自己能力扩充这个游戏,不过别把游戏弄得太诡异了。
- 笨方法更简单
- 习题 00: 准备工作
- 习题 01: 第一个程序
- 习题 02: 注释和#号
- 习题 03: 数字和数学计算
- 习题 04: 变量的命名
- 习题 05: 更多的变量和输出
- 习题 06: 字符串和文字
- 习题 07: 更多输出
- 习题 08: 输出,输出
- 习题 09: 输出,输出,输出~
- 习题 10: 那是啥?
- 习题 11: 提问
- 习题 12: 模块
- 习题 13: 参数,解包,参数
- 习题 14: 提示和传递
- 习题 15: 读取文件
- 习题 16: 操作文件
- 习题 17: 更多的文件操作
- 习题 18: 命名,变量,代码,函数
- 习题 19: 函数和变量
- 习题 20: 函数和文件
- 习题 21: 函数可以传入信息
- 习题 22: 到现在你学到了什么?
- 习题 23: 阅读一些代码
- 习题 24: 更多练习
- 习题 25: 更多更多的练习
- 习题 26: 恭喜你,现在来考试了!
- 习题 27: 记住逻辑关系
- 习题 28: Boolean表达式练习
- 习题 29: 如果
- 习题 30: Else 和 If
- 习题 31: 做出判断
- 习题 32: 循环和数组
- 习题 33: While
- 习题 34: 存取数组里的元素
- 习题 35: 分支和函数
- 习题 36: 设计和测试
- 习题 37: 重视各种符号