目前,我一直致力于 Chris Pine 学习编程 ruby 的实验室项目,其中我应该添加与类似 tamogachi 的程序完全交互的能力。我一直在想,我可以使用 name = gets.chomp
为宠物名称定义一个变量并将其标记为 pet = Dragon.new name
但是当我这样做时并执行程序,它只是通过宠物的所有不同方法,然后不接受输入(我正在使用带有用户选项的 while 循环)。
实验室项目问题
编写一个程序,以便您可以与您的小龙互动。您应该能够输入诸如 feed
和 walk
之类的命令,并在您的龙上调用这些方法。当然,由于您输入的只是字符串,因此您必须进行某种方法分派(dispatch),您的程序会在其中检查输入的是哪个字符串,然后调用适当的方法。
class Dragon
def initialize name
@name = name
@asleep = false
@stuffInBelly = 10 # He's full.
@stuffInIntestine = 0 # He doesn't need to go.
puts @name + ' is born.'
end
def feed
puts 'You feed ' + @name + '.'
@stuffInBelly = 10
passageOfTime
end
def walk
puts 'You walk ' + @name + '.'
@stuffInIntestine = 0
passageOfTime
end
def putToBed
puts 'You put ' + @name + ' to bed.'
@asleep = true
3.times do
if @asleep
passageOfTime
end
if @asleep
puts @name + ' snores, filling the room with smoke.'
end
end
if @asleep
@asleep = false
puts @name + ' wakes up slowly.'
end
end
def toss
puts 'You toss ' + @name + ' up into the air.'
puts 'He giggles, which singes your eyebrows.'
passageOfTime
end
def rock
puts 'You rock ' + @name + ' gently.'
@asleep = true
puts 'He briefly dozes off...'
passageOfTime
if @asleep
@asleep = false
puts '...but wakes when you stop.'
end
end
private
# "private" means that the methods defined here are
# methods internal to the object. (You can feed
# your dragon, but you can't ask him if he's hungry.)
def hungry?
# Method names can end with "?".
# Usually, we only do this if the method
# returns true or false, like this:
@stuffInBelly <= 2
end
def poopy?
@stuffInIntestine >= 8
end
def passageOfTime
if @stuffInBelly > 0
# Move food from belly to intestine.
@stuffInBelly = @stuffInBelly - 1
@stuffInIntestine = @stuffInIntestine + 1
else # Our dragon is starving!
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + ' is starving! In desperation, he ate YOU!'
exit # This quits the program.
end
if @stuffInIntestine >= 10
@stuffInIntestine = 0
puts 'Whoops! ' + @name + ' had an accident...'
end
if hungry?
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + '\'s stomach grumbles...'
end
if poopy?
if @asleep
@asleep = false
puts 'He wakes up suddenly!'
end
puts @name + ' does the potty dance...'
end
end
end
name = gets.chomp
pet = Dragon.new name
usrin = ''
while usrin != 'exit'
feed = pet.feed
toss = pet.toss
walk = pet.walk
rock = pet.rock
bed = pet.putToBed
usrin = gets.chomp
end
(如果仅从 usrin = gets.chomp
调用其中一个方法,则替代示例获得相同的输出)
name = gets.chomp
pet = Dragon.new name
usrin = ''
feed = pet.feed
toss = pet.toss
walk = pet.walk
rock = pet.rock
bed = pet.putToBed
while usrin != 'exit'
usrin = gets.chomp
end
如果有人能帮我解决这个问题,这样我就可以停止像
这样的输出name is born.
you feed name.
you toss name up into the air.
he giggles, which singes your eyebrows.
you walk name.
you rock name gently.
he briefly dozes off....
...but wakes when you stop.
you put name to bed.
name snores, filling the room with smoke.
name snores, filling the room with smoke.
name snores, filling the room with smoke.
name wakes up slowly.
非常感谢,因为我仍在学习我的第一门编程语言,这有点令人沮丧。
程序的期望输出是当用户输入命令时对您的宠物采取正确的操作,例如“喂食”,当 usrin = gets.chomp
在循环中提示时>.
最佳答案
您需要为各种方法调用添加条件。
puts "Enter pet's name: "
usrin = gets.chomp
pet = Dragon.new usrin
while usrin != 'exit'
pet.feed if usrin == 'feed'
pet.toss if usrin == 'toss'
pet.walk if usrin == 'walk'
pet.rock if usrin == 'rock'
pet.putToBed if usrin == 'bed'
puts "What next? Choose one - feed,toss,walk,rock,putToBed"
usrin = gets.chomp
end
或者,您可以利用 Ruby 的 Object#send
来简化循环。因为它允许您在知道方法名称的情况下调用方法。
puts "Enter pet's name: "
usrin = gets.chomp
pet = Dragon.new usrin
loop do
puts "What next? Options: feed, toss, walk, rock, putToBed or exit"
usrin = gets.chomp
break if usrin == "exit"
pet.send(usrin) rescue puts "Invalid input"
end
只有当您有一个与选项文本匹配的方法时,上述解决方案才有效。
关于ruby - 将类和方法与用户输入一起用于交互式程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417655/