2016-03-31 161 views
1

我有一個case構造,用於檢查標準用戶輸入。如果給定的答案不是用戶給出的,我想重複(或者什麼都不做)。我有case構造的五個選項:"s","t","c","a""o"。如果用戶沒有放棄這五個答案,我想重複(或者什麼也不做)。如果沒有給出正確的答案,請重複案例

choice = gets.chomp.downcase 

case choice 
when 's' 
    @klass = "S Klass" 
    @extra = "S Extra"    
    puts "S Story" 
when 't' 
    @klass = "T Klass" 
    @extra = "T Extra" 
    puts "T Story" 
when 'c' 
    @klass = "C Klass" 
    @extra = "C Extra" 
    puts "C Story" 
when 'a' 
    @klass = "A Klass" 
    @extra = "A Extra" 
    puts "A Story" 
when 'o' 
    @klass = "O Klass" 
    @extra = "O Extra" 
    puts "O Story" 
end 

回答

4
loop do 
    choice = gets.chomp.upcase 
    case choice 
    when "S", "T", "C", "A", "O" 
    @klass = "#{choice} Klass" 
    @extra = "#{choice} Extra"    
    puts "#{choice} Story" 
    break 
    end 
end