簡體   English   中英

獨立方法本身正在調用另一個方法

[英]Stand alone method is calling another method by itself

好的,每當我想從方法中返回一些東西時,我都會認真地將方法傳遞給方法。 你們能解釋一下我如何通過它。 這是我的哈希

$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”。 而不是結束問題,它返回到ZombieRoom並繼續循環播放“ Type Shoot將其擊倒或退出”。 至少有人可以告訴我解決此問題的正確方法還是通過其他方法傳遞方法嗎?

在紅寶石常量中,以大寫字母開頭。 方法始終以小寫形式定義。

在irb中嘗試這個

irb(main):001:0> def Weapon
irb(main):002:1> end
=> :Weapon
irb(main):003:0> Weapon
NameError: uninitialized constant Weapon

要使用ruby的命名約定解決您的問題名稱方法:
zombie_roomcollect_prize等。

然后這段代碼將起作用:
zombie_room(weapon())

您在做什么並沒有真正將方法武器傳遞給方法僵屍室。 真正發生的是執行了方法武器,然后返回一個值,並將該值的結果傳遞給方法zombie_room。

我認為這就是您想要的。

如果需要傳遞方法,請查看proclambda文檔,或者僅使用塊。

您的代碼處於一個大while true循環中。 由於true始終為true,因此它將永遠不會結束,因此在調用CollectPrize()之后,它僅返回while語句。

您可以通過在CollectPrize()之后插入一個break來擺脫掉它,但是圍繞此循環還有另一個while true循環。

我認為您需要更加注意如何退出while循環。

puts "Time to take down that zombie now."
while true # <---------------- this is ALWAYS going to loop, without end
  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM