我有一個hash值,用於鍵值和cat實例的cat名稱。有沒有辦法使得當人們鍵入response
不在cats
散列中的密鑰中時,終端將重新打印puts "Which cat would you like to know about?"
問題或鍵入:「再試一次」?我想我正在問一些「while ... else」。Ruby中是否存在「while ... else」?
puts "Which cat would you like to know about?"
puts cats.keys
response = gets.chomp
while cats.include?(response)
puts "The cat you chose is #{cats[response].age} old"
puts "The cat you chose is named #{cats[response].name}"
puts "The cat you chose is a #{cats[response].breed} cat"
puts "Is there another cat would you like to know about?"
response = gets.chomp
end
答案是** no **。可能你需要在'if'中包裝'while'的主體,或者類似的東西。 – Augusto
你有一個不符合語法的句子'有沒有...'在放入。 – sawa
'while ... else'是Python所具有的,我希望Ruby也可以。 –