繁体   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