当输入为:
时,我正试图退出while循环BYE
BYE
BYE
而不是这个:
BYE BYE BYE
代码是:
my_reply = ((word1 = gets) + (word2 = gets) + (word3 = gets))
while my_reply != ((word1 == 'BYE') and (word2 == 'BYE') and (word3 == 'BYE'))
puts 'Grandma: ...'
my_reply = ((word1 = gets) + (word2 = gets) + (word3 = gets))
end
puts 'Grandma: ' + 'Fine, sonny, don\'t hang out with your aging grandmother!'.upcase
哪个不行。我需要帮助。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
获取的输入包括换行符(BYE \ n)。 gets.chomp 可以摆脱它。
答案 2 :(得分:0)
我不太确定,但您可能正在尝试做类似的事情:
bye_3 = Array.new(3, "BYE")
until Array.new(3){gets.chomp} == bye_3
puts "Grandma: ..."
end
puts 'Grandma: ' + 'Fine, sonny, 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]
相同。