2012-09-16 28 views
1

我有一個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 
+0

答案是** no **。可能你需要在'if'中包裝'while'的主體,或者類似的東西。 – Augusto

+0

你有一個不符合語法的句子'有沒有...'在放入。 – sawa

+0

'while ... else'是Python所具有的,我希望Ruby也可以。 –

回答

1

就我所知,沒有「while ... else」。如果你不介意的循環持續不管響應是否是一個有效的貓的名字或沒有,也許這會爲你工作:

puts "Which cat would you like to know about?" 
puts cats.keys 

while true 
    response = gets.chomp 
    if response.empty? 
    break 
    elsif 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?" 
    else 
    puts "There is no cat with that name. Try again." 
    end 
end 

這將反覆提示的寵物貓名的用戶,直到用戶響應用一個空字符串,在這一點上,它將跳出循環。

+0

無論結果如何,這不會永久循環嗎?你不需要休息嗎? (我不知道紅寶石) – Tudor

+0

@Tudor:的確如此。我現在增加了一個休息條件。 :-) – skunkfrukt

+0

@Tudor通常情況下,你確實需要休息一下,但這不是OP想要的。 – sawa

0

你可以使用一個額外的問題,重新安排你的代碼:

cats = {'a' => 1} #testdata 
continue = true #set a flag 

while continue 
    puts "Which cat would you like to know about?" 
    response = gets.chomp 
    while cats.include?(response) 
    puts cats[response] 
    puts "Is there another cat would you like to know about?" 
    response = gets.chomp 
    end 
    puts "Try another? (Y for yes)" 
    continue = gets.chomp =~ /[YyJj]/ #Test for Yes or yes, J for German J... 
end 
0

它已經有一段時間,因爲我已經用Ruby玩,但這樣的事情出現在腦海:

def main 
    print_header 
     while true 
      resp = get_response 
      if cats.include?(resp) 
       print_info(resp) 
      else 
       print_header 
     end 
end 

def get_response 
    puts cats.keys 
    response = gets.chomp 
end 

def print_header 
    puts "Which cat would you like to know about?" 
end 

def print_info 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?" 
end 

請注意你需要一個終點。如果get_response返回「否」,則退出。

0
continuous = false 
loop do 
    unless continuous 
    puts "Which cat would you like to know about?", cats.keys 
    else 
    puts "Is there another cat would you like to know about?" 
    end 
    if cat = cats[gets.chomp] 
    puts "The cat you chose is #{cat.age} old" 
    puts "The cat you chose is named #{cat.name}" 
    puts "The cat you chose is a #{cat.breed} cat" 
    continuous = true 
    else 
    continuous = false 
    end 
end 
+0

好的小程序,但遞歸可能會吃掉你的堆棧... – DGM

+0

@DGM改變循環,跟隨你的評論。 – sawa