2014-02-21 179 views
0

我正處於學習Ruby的早期階段,並在嘗試爲我的某個程序創建條件語句時遇到了問題。如果orignal不符合前兩個條件的條件,我基本上希望它回滾並獲得新的輸入值。在if/else語句中創建循環

因此,例如:

puts "Choose either the 'red' or 'blue' pill" 

choice = gets.to_s.downcase.chomp 

if choice == red 
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
elsif choice == "blue" 
    puts "The story ends, you wake up in your bed and believe whatever you want to believe" 
else 
    puts "You have to choose one" 
end 
+0

是'red'變量?如果不是,'choice == red'將會失敗。重要的是你的源代碼可以運行。 –

回答

3

這裏的另一種常用的結構:

loop do 
    puts "Choose either an 'upper' or a 'downer'" 

    case gets.downcase.chomp 
    when "upper" 
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
    break 
    when "downer" 
    puts "The story ends, you wake up and believe whatever you want to believe" 
    break 
    else 
    puts "You have to choose one" 
    end 
end 
+1

在這裏使用'#loop'很好用。但'gets.to_s.downcase.chomp'可以寫成'gets.downcase.chomp'。 –

+1

謝謝,@Arup,我只是一味的剪貼。對我感到羞恥!將解決。 –

1
begin 
    puts "Choose either the 'red' or 'blue' pill" 
    choice = gets.to_s.downcase.chomp 

    if choice == "red" 
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
    elsif choice == "blue" 
    puts "The story ends, you wake up in your bed and believe whatever you want to believe" 
    else 
    puts "You have to choose one" 
    choice = "invalid" 
    end 
end while(choice == "invalid") 

控制檯輸出:

Choose either the 'red' or 'blue' pill 
#empty input 
You have to choose one 
Choose either the 'red' or 'blue' pill 
red 
Fasten your seatbelt dorothy 'cause kansas is going bye-bye 
=> nil 
+0

沒有'String#blank?'這樣的方法。即使有,這也行不通。 – Linuxios

+0

'空白?'是一個鐵軌的事情。 – DGM

+0

@DGM是的,空白?是軌道。我改變了它。 – usha

0
class WrongChoice < StandardError 
end 

puts "Choose either the 'red' or 'blue' pill" 

begin 
    choice = gets.to_s.downcase.chomp 
    raise WrongChoice.new unless ['blue', 'red'].include? choice 
    if choice == red 
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
    else 
    puts "The story ends, you wake up in your bed and believe whatever you want to" 
    end 
rescue WrongChoice 
    puts "You have to choose one" 
    retry 
end 
+1

雖然我不會爲此使用異常,但重試非常優雅。 – Linuxios

1

如何Ruby的throwcatch對於這種情況:

def ask 
    puts "Choose either the 'red' or 'blue' pill" 
    choice = gets.downcase.chomp 
    if choice == 'red' 
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
    elsif choice == "blue" 
    puts "The story ends, you wake up in your bed and believe whatever you want to believe" 
    else 
    puts "You have to choose one" 
    throw :done,ask 
    end 
end 

catch(:done) do 
    ask 
end 

允許運行代碼:

(arup~>Ruby)$ ruby -v a.rb 
ruby 2.0.0p0 (2013-02-24 revision 39474) [i686-linux] 
Choose either the 'red' or 'blue' pill 
foo 
You have to choose one 
Choose either the 'red' or 'blue' pill 
bar 
You have to choose one 
Choose either the 'red' or 'blue' pill 
blue 
The story ends, you wake up in your bed and believe whatever you want to believe 
(arup~>Ruby)$