我正在让自己和一些我的朋友成为龙与地下城的战斗助理,因为大部分保持轨道都是重复的,所以我认为我可以做些Ruby。它进展顺利,但现在我遇到了障碍。未定义的方法:变量?
这是我的代码
def party8
party7
puts "Last one! What's your eighth player's name?"
player8name = gets.chomp
puts "What's their AC?"
player8ac = gets.chomp.to_i
puts "Got it. What's their max HP?"
player8maxhp = gets.chomp.to_i
end
def partysetup
puts "hi"
if 8 == playercount
party8
else
party1
end
end
#intro----------------------------------------------------------------------
puts "-Hello. I am l1fecount, the DM's combat assistant."
puts "-Before we begin, would you like to see in-depth information about me?"
infoq = gets.chomp
infoq.downcase!
if infoq == "yes"
puts "-Very well, I'm glad to explain. I am l1fecount, a program designed to keep track of up to 5 types of mobs, with up to 10
of each. I can also keep track of up to 8 players. I keep track of turn order, current HP vs max HP, CR, and armor
class. I am still very young, so please be patient with me. ^^; "
else
puts "-Right then."
end
puts "-So, let's begin."
#intro end----------------------------------------------------------------
#party---------------------------------------------------------------------
loop do
puts "How many players today?"
playercount = gets.chomp.to_i
if 0 >= playercount
puts "You can't have no players in a party. That's not D&D, that's you having no friends."
redo
elsif 8 < playercount
puts "Hey now, that's a huge party. I can only handle eight players at once."
redo
elsif 8 >= playercount
break
else
puts "A number between 1 and 8, please."
redo
end
end
partysetup
`
(party1-7存在,但等同于派对8,所以我不包括它简洁的缘故。)
它运行得很好,直到我尝试运行partysetup。我加了把语句,这样我可以看到,如果正被调用的方法,它是,但我不断收到这样的:
-Hello. I am l1fecount, the DM's combat assistant.
-Before we begin, would you like to see in-depth information about me?
no
-Right then.
-So, let's begin.
How many players today?
8
hi
Error: undefined method `playercount' for main:Object
我试图寻找简单的拼写错误,转换playercount为字符串或符号,但没有解决这个问题。请帮助?
尝试在循环之前放置'playercount = nil'。否则,当您在循环内定义变量时,引用将仅在循环中局部。 –
请阅读“[mcve]”,包括链接的页面。您的代码无法运行以证明问题,为了帮助您,我们必须对其进行修改。您需要帮助我们通过将代码剥离到最低程度来帮助您。 http://ericlippert.com/2014/03/05/how-to-debug-small-programs/有助于解释它。 –
请参阅[差异之间的各种变量范围在红宝石](http://stackoverflow.com/questions/11495098/difference-between-various-variables-scopes-in-ruby)以及 –