0

好的,每当我想从方法中返回一些东西时,我都非常讨厌将方法传递给方法。你们能解释一下我如何通过它。这是我的哈希

$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]
}

这是我的方法 Weapon 的代码

    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

在调用该方法时。用户将选择他的武器,并将其添加到 $choosen_gun 散列中,其编号为 num,并返回用户键入的 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 并继续在“键入 Shoot 以将其击倒或退出”处循环。有人至少可以告诉我解决此问题的正确方法,或者还有什么其他方法可以传递方法?

4

2 回答 2

1

在 ruby​​ 中,常量以大写字母开头。方法始终以小写形式定义。

在 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或仅使用块。

于 2017-01-11T14:01:22.047 回答
1

您的代码处于一个大while true循环中。由于 true 始终为 true,它永远不会结束,因此在调用 CollectPrize() 之后,它只是返回到while语句。

break您可以通过在之后插入一行来摆脱它,CollectPrize()但围绕这一行还有另一个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
于 2017-01-11T14:40:11.430 回答