【发布时间】:2015-06-01 05:49:13
【问题描述】:
在http://learnrubythehardway.org/book/ex35.html 的第 7 行,有一条评论指出存在错误。
当这个程序运行并且你选择一个既不是“0”也不是“1”的数字时,它会说它不是一个数字。如何让它检测所有数字?
代码如下:
def gold_room
puts "This room is full of gold. How much do you take?"
print "> "
choice = $stdin.gets.chomp
# this line has a bug, so fix it
if choice.include?("0") || choice.include?("1")
how_much = choice.to_i
else
dead("Man, learn to type a number.")
end
if how_much < 50
puts "Nice, you're not greedy, you win!"
exit(0)
else
dead("You greedy bastard!")
end
end
【问题讨论】:
-
你能发布你迄今为止尝试过的东西吗?否则,它只是为你做功课。
-
这里有一些关于 Ruby 模式匹配运算符的好信息:stackoverflow.com/questions/3025838/…
标签: ruby if-statement