问题描述
这个问题已经在这里有了答案:
解决方法
gets
还返回换行符和回车符\“ \\ r \\ n \”,例如\“再见\\ r \\ n \”。这意味着您必须先将它们删除,然后再与其他字符串进行比较。 (请参阅String.strip)
while [gets,gets,gets].map(&:strip) != [\'BYE\',\'BYE\',\'BYE\']
puts \'Grandma: ...\'
end
puts \'Grandma: \' + \'Fine,sonny,don\\\'t hang out with your aging grandmother!\'.upcase
, gets的输入包括换行符(BYE \\ n)。做gets.chomp摆脱它。
, 我不太确定,但也许您正在尝试执行以下操作:
bye_3 = Array.new(3,\"BYE\")
until Array.new(3){gets.chomp} == bye_3
puts \"Grandma: ...\"
end
puts \'Grandma: \' + \'Fine,don\\\'t hang out with your aging grandmother!\'.upcase
Array.new(3,\"BYE\")
与[\"BYE\",\"BYE\",\"BYE\"]
相同,Array.new(3){gets.chomp}
与[gets.chomp,gets.chomp,gets.chomp]
相同。