好吧,我認真地吮吸傳遞方法,只要我想從方法返回一些東西。你們能解釋一下我怎麼去傳遞它。 這裏是我的散列獨立方法本身調用另一種方法
$choosen_gun = {}
$Weapon = {
:Bazoka => ["Bazoka",5],
:Machine_gun => ["Machine_gun",1000],
:Hand_gun => ["Hand_gun",24,2],
:Double_Hand_gun => ["Double_Hand_gun",24,4],
:Sniper => ["Sniper",12,1],
:Shot_gun => ["Shot_gun",8,2]
}
這裏是我的方法支招
def Weapon
puts "Now it's time to select your weapon."
puts "Please choose a weapon that is good throughout the game."
puts "Whenever you are shortage of bullets, please reload it."
puts "Please avoid last minute of reloading of weapon."
puts "Now choose your weapon based on your own preferences."
print "\n"
puts "Type 1"
puts "Gun Name: Bazoka"
puts "Description: A powerful gun that is strong with only 5 bullets."
puts "Rating: ★ ★ ★ ★"
num = gets.chomp.to_i
case num
when 1
puts "Selection of Bazoka is chosen"
puts "Loaded 5 bullets only"
$choosen_gun[num] = $Weapon[:Bazoka]
end
return num
end
代碼在調用該方法。用戶會選擇他的武器,並與它的NUM將其添加到$ choosen_gun哈希值,它的回報是NUM什麼類型的用戶
這裏是我的方法的代碼ZombieRoom
def ZombieRoom(w)
zombie = {
:Construcied => [5],
:Invader => [5],
:Damned => [5],
:Steampunk => [5],
:Stoner => [5],
:Wasted => [5],
:Romero => [5]
}
puts "Welcome to the worst night mare of Zombie Room"
puts "You will be fighting with a random zombie"
while true
puts ".........."
puts "Selecting a random zombie"
puts "Selecting your prefered gun...."
case w
when 1
$choosen_gun[1]
puts "Your selected gun is #{$choosen_gun[1][0]}"
#values = zombie.values
#puts values[rand(values.size)]
#random_zombie = zombie.keys.sample(1)
#puts random_zombie[
random_zombie = zombie.to_a.sample(1).to_h
random_zombie.each do |key,value|
puts "Your random zombie is #{key}"
puts "With a health value of #{value[0]}"
puts "Time to take down that zombie now."
while true
puts "Type Shoot to knock it down or quit."
choice = gets.chomp
if $choosen_gun[1][1] >= 1
health = value[0] -= 1
$choosen_gun[1][1] -= 1
puts "#{key} health now is #{health}"
else
puts "Please reload your gun"
puts "Reloading......"
$choosen_gun[1][1] += 5
end
if health == 0
puts "You have defeated #{key}"
puts "Congrats!!!"
puts "We are happy for you"
puts "Lets begins to collect your prize"
CollectPrize()
else
puts "You did not defeat the #{key} yet"
end
end
end
end
end
end
這裏是我的代碼方法CollectPrize
def CollectPrize
puts "Congratulations on defeating"
puts "We would now like to give you some case prizes"
print "\n"
puts "Please choose only 1 prize for yourself"
print "\n"
puts "Type 1"
puts "$50,000"
print "\n"
puts "Type 2"
puts "$25,000"
print "\n"
puts "Type 3"
puts "$55,000"
hoho = gets.chomp.to_f
if hoho == 1
puts "hehe"
end
end
這裏,我怎麼叫我的方法
ZombieRoom(Weapon())
CollectPrize()
現在的問題是每當CollectPrize方法被調用時,我輸入我的輸入來收集獎勵示例1,然後打印「$ 50,000」。而不是結束這個問題,它回到了殭屍房間,並繼續在「類型射擊擊倒或退出」循環。有人能告訴我一個正確的方法來解決這個問題,或者還有什麼其他的方式來傳遞一個方法嗎?
你的問題還不清楚。 「傳遞方法到方法」是什麼意思?從我所看到的,你永遠不會傳遞一個方法到任何地方,所以爲什麼這是相關的?爲什麼你需要這樣做「每當我想從方法中返回一些東西」? 「你們能解釋一下我怎樣才能通過它」。參考?另外,請提供[mcve],並特別注意「M」(最小)部分。我非常懷疑你需要120行代碼來複制你的問題。事實上,我發現了幾個與您的問題完全無關的錯誤,但使它變得非常... –
...無法診斷。 –
閱讀最後的陳述,你就會明白。 – Suresh