我可以在`case`语句中插入`if`条件吗?

时间:2016-01-23 21:21:24

标签: ruby

我正在努力建立一个游戏。我想根据用户的选择为用户提供选项。我的代码必须是case语句:

puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options: hight, Who is Superman, Bake a pie "
option = gets.chomp
case option
when "hight"
  puts "Lets see your hight yo!"
  option_1 = gets.chomp
  case option_1
  when
  end
end

如果他们选择Hight,

if i >= 5 ft
  puts "you may live"
else
  puts "you in trouble"

非常欢迎使用case的任何其他替代方案。

3 个答案:

答案 0 :(得分:1)

是的,您可以在case语句中插入条件。

答案 1 :(得分:0)

您可以从case语句中调用一个函数,并将if条件放在函数中。除非你有充分的理由不这样做?

答案 2 :(得分:-1)

是的,你可以,我想(尽量不要过多地改变你的代码)你可以做这样的事情:

puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options are: height, Who is Superman or Bake a pie "
option = gets.chomp

case option
when "height"
  puts "Lets see your height yo!"
  option_1 = gets.chomp.to_i
  if option_1 >= 5
    puts "you may live"
  else
    puts "you're in trouble"
  end
when "Who is Superman"
    #code
else
    #code  
end