2016-12-29 46 views
-4
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 

紅寶石battle_loot_calc.rb
battle_loot_calc.rb:12:語法錯誤,意想不到的< =
ELSIF CR> = 4 & & < = 8爲什麼我在紅寶石獲得此,如果其他錯誤

+0

語言請。 –

回答

1

添加cr以下&&像這樣:

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

1

你可以使用情況與範圍:

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! 
+0

謝謝大家的快速反應和有用的信息! – didit4tehlawlz