【问题标题】:Using Classes and methods with user input for an interactive program在交互式程序中使用具有用户输入的类和方法
【发布时间】:2016-03-28 19:47:28
【问题描述】:

目前,我一直在研究 Chris Pine 的“学习编程 ruby​​”的实验室项目,使用的示例是我应该添加与类似 tamogachi 的程序完全交互的能力。我一直在想,我可以使用name = gets.chomp 为宠物名称定义一个变量,并将其标记为pet = Dragon.new name,但是当我这样做并执行程序时,它只是通过了宠物的所有不同方法然后不会接受输入(我正在使用带有用户选项的 while 循环)。

实验室项目问题 编写一个程序,以便您可以与您的小龙互动。您应该能够输入诸如feedwalk 之类的命令,并在您的龙上调用这些方法。当然,由于您输入的只是字符串,因此您必须进行某种方法调度,让您的程序检查输入的字符串,然后调用适当的方法。

 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 在循环中提示时,对您的宠物采取正确的操作。

【问题讨论】:

  • 好吧,你调用循环内的所有方法。这就是他们被称为的原因。解决方案?不要调用它们。
  • @Sergio Tulentsev 不幸的是,无论它们是在循环内定义还是未在所有输出中定义,当仅调用其中一种方法时,仍然会打印底部的输出。我也不知道将它们定义为变量会打印类中的每个方法,这就是为什么我不知道出了什么问题。
  • 不确定您想说什么,但您在每次循环迭代中无条件调用所有操作,然后再请求用户输入。而用户输入本身 - 它被忽略了。
  • 同样的事情。调用所有方法,忽略输入。你认为当你这样做时会发生什么,例如feed = pet.feed
  • 期望的输出是什么?

标签: ruby class oop methods


【解决方案1】:

需要为各种方法调用添加条件。

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

仅当您有与选项文本匹配的方法时,上述解决方案才有效。

【讨论】:

  • 在节目期间宠物的名字不应该改变,这是有意的。
  • 那些本地变量没用,顺便说一句
  • @SergioTulentsev 是的,他们是
  • 那么,如果我想输入feed 并让宠物喂食,我不相信我理解它们的正确用途,那么应该使用什么?
  • @TaylorSwanson 我已经为您的问题提供了解决方案。不知道现在似乎是什么混乱
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
相关资源
最近更新 更多