为什么我在ruby中得到这个if else错误

时间:2016-12-29 20:14:38

标签: ruby debugging syntax-error

shit_loot = ['Worn Dagger', 'Dirty Panties', 'Broken Staff', 'Bear Claw', 'Used Bandage']
decent_loot = ['Simple Staff', 'Alchemy Bag', 'Mask of Emptiness', 'Cloak of Disappearance', 'Large Health Potion']
epic_loot = ['Sword of 1000 Truths', 'The Master Sword', 'BFG', 'The Fate of the World', 'Infinite Bag of Infinity']

puts "On a scale of 1 - 10 how hard was the battle for the party?"
cr = gets.chomp.to_i
puts "Your party completed a challenge rating #{cr} battle, Great Work!"

if cr <= 3 
  puts "Your loot is #{shit_loot.sample}, #{shit_loot.sample}, #{shit_loot.sample}. Grats on the shitty loot!"

  elsif cr >= 4 && <= 8
    puts "Your loot is #{decent_loot.sample}, #{decent_loot.sample}, #{decent_loot.sample}. Grats on the decent loot!"

  else cr > 8
    puts "Your loot is #{epic_loot.sample}, #{epic_loot.sample}, #{epic_loot.sample}. Grats on the epic loot!"
end

ruby​​ battle_loot_calc.rb
battle_loot_calc.rb:12:语法错误,意外&lt; =
  elsif cr&gt; = 4&amp;&amp; &lt; = 8

2 个答案:

答案 0 :(得分:1)

cr之后添加&&,如下所示:

elsif cr >= 4 && cr <= 8 puts ...

答案 1 :(得分:1)

您可以使用带范围的案例:

1 + ((row_number() over (order by TimeStamp, FileName, OrderID) - 1) / 5) AS [Row_Number]

所以你的代码变成了:

adjective = case cr
when (0..3) then 'shit'
when (4..8) then 'decent'
when (9..10) then 'epic'
end