为什么在ruby中会出现if-else错误 shit_loot=[‘磨损的匕首’、‘脏内裤’、‘断杖’、‘熊爪’、‘用过的绷带’] 体面的战利品=['简单的棍棒','炼金术袋','空虚面具','消失斗篷','大药水'] 史诗战利品=[“1000条真理之剑”、“大师之剑”、“BFG”、“世界的命运”、“无限之袋”] 将“该党的战斗有多艰难?” cr=get.chomp.to_i “你的队伍完成了一场挑战评级#{cr}战,干得好!” 如果cr=4&&8 放上“你的战利品是”{epic_loot.sample}、{epic_loot.sample}、{epic_loot.sample}、{epic_loot.sample}。抓取史诗战利品!” 结束

为什么在ruby中会出现if-else错误 shit_loot=[‘磨损的匕首’、‘脏内裤’、‘断杖’、‘熊爪’、‘用过的绷带’] 体面的战利品=['简单的棍棒','炼金术袋','空虚面具','消失斗篷','大药水'] 史诗战利品=[“1000条真理之剑”、“大师之剑”、“BFG”、“世界的命运”、“无限之袋”] 将“该党的战斗有多艰难?” cr=get.chomp.to_i “你的队伍完成了一场挑战评级#{cr}战,干得好!” 如果cr=4&&8 放上“你的战利品是”{epic_loot.sample}、{epic_loot.sample}、{epic_loot.sample}、{epic_loot.sample}。抓取史诗战利品!” 结束,ruby,debugging,syntax-error,Ruby,Debugging,Syntax Error,红宝石战利品计算rb battle_loot_calc.rb:12:语法错误,意外=4&&&添加cr如下&&类似: elsif cr>=4&&cr您可以对范围使用case: shit_loot = ['Worn Dagger', 'Dirty Panties', 'Broken Staff', 'Bear Claw', 'Used Bandage'] decent_loot = ['Simple Staff', 'Alchemy Bag', 'Mask of Emptiness', 'Cloak

红宝石战利品计算rb

battle_loot_calc.rb:12:语法错误,意外=4&&&添加
cr
如下
&&
类似:


elsif cr>=4&&cr您可以对范围使用case:

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
因此,您的代码变成:

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

谢谢你们的快速回复和有用的信息!
possible_loots = {
  'shitty' => ['Worn Dagger', 'Dirty Panties', 'Broken Staff', 'Bear Claw', 'Used Bandage'],
  'decent' => ['Simple Staff', 'Alchemy Bag', 'Mask of Emptiness', 'Cloak of Disappearance', 'Large Health Potion'],
  'epic' => ['Sword of 1000 Truths', 'The Master Sword', 'BFG', 'The Fate of the World', 'Infinite Bag of Infinity']
} 

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

loot = (1..3).map{ possible_loots[adjective].sample}.join(', ')
puts "Your loot is #{loot}. Grats on the #{adjective} loot!"

# cr = 5
#=> Your loot is Cloak of Disappearance, Simple Staff, Alchemy Bag. Grats on the decent loot!
# cr = 10
#=> Your loot is Infinite Bag of Infinity, BFG, The Master Sword. Grats on the epic loot!