1
如果if語句轉到'else',可以讓重複if語句嗎?如果轉到'else',重複if語句
這是一個代碼的一部分:
puts "While you are walking you find a small jar containing honey. Do
you take it? yes/not"
choice = $stdin.gets.chomp
if choice.include?("yes")
honey = true
puts " "
puts "You put the small honey jar in your bag and then keep walking."
elsif choice.include?("not")
puts "Ok! maybe you are right. Better leave it!"
puts "You keep going"
honey = false
else
" "
puts "Answer yes or not."
end
所以我想,如果用戶不是或不是那種if語句再次運行,可能再次提出的問題或只是給了「其他'的信息,並再次給出寫出答案的可能性。謝謝。
感謝的提示。但是,在這種情況下,我決定使用'while'而不是'loop do',因爲在循環中我需要修改局部變量並在循環之後使用它。使用'while'這可能與'循環做'這不是。本地變量不會被修改。 –
@MarcoVanali:它仍然是一個循環:)你也可以看看埃裏克的答案。很有用。 –