虽然循环不起作用。包括字符串并转换为大写

While loop doesn't work. Includes strings and converting to upper case

我无法找出这段代码的问题:

select = false
while (!select)
  print "What animal do you want to adopt? (Cat/Dog/Fish): "
  your_animal = gets
  if your_animal.upcase == "CAT" or your_animal.upcase == "DOG" or your_animal.upcase == "FISH"
    puts "Ah, you want #{your_animal}."
    select = true
  else
    puts "Please pick from any of the three animals and make sure it is spelled correctly."
  end
end

三个条件的if语句是行不通的,即使我将if语句的正确响应写到else语句中,也会执行else语句中的代码被激活。

如有任何帮助,我们将不胜感激。

改变

your_animal = gets

your_animal = gets.chomp

原因是gets return是整个字符串输入,包括终止回车return.

旁注:而不是

your_animal.upcase == "CAT" or 
  your_animal.upcase == "DOG" or
  your_animal.upcase == "FISH"

可能会用到:

%w(CAT DOG FISH).include? your_animal.upcase