2011-06-14 6 views
0

일치하지 않습니다입력 조건 내가 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

http://stackoverflow.com/q/3955688/38765 스크립트를 디버깅하는 방법에 대한 자세한 정보가 있습니다. –

답변

1

gets 또한 줄 바꿈과 캐리지 리턴 "\ r \ n"을 반환합니다. "BYE \ 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 
+0

감사합니다, 매트! – sudostack

0

의 입력이이되면 줄 바꿈 (BYE \ n)이 포함됩니다. gets.chomp을 제거하십시오.

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]과 동일합니다.

관련 문제