2016-12-29 2 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 

Rubin battle_loot_calc.rb
battle_loot_calc.rb: 12: Syntaxfehler, unerwartete < =
elsif cr> = 4 & & < = 8Warum bekomme ich dies, wenn sonst Fehler in Ruby

+0

Sprache bitte. –

Antwort

1

In cr&& folgende wie folgt:

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

1

Sie könnten Fall verwenden, um mit Bereichen:

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

So Ihr Code wird:

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

Vielen Dank für die schnellen Antworten und die hilfreichen Informationen! – didit4tehlawlz

Verwandte Themen