我正在讓自己和一些我的朋友成爲龍與地下城的戰鬥助理,因爲大部分保持軌道都是重複的,所以我認爲我可以做些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)以及 –