Ruby 将数组作为参数传递给方法会导致0:1错误,或者是空数组
我正在测试一个单人21点程序。我制作了一个Ruby 将数组作为参数传递给方法会导致0:1错误,或者是空数组,ruby,methods,arguments,Ruby,Methods,Arguments,我正在测试一个单人21点程序。我制作了一个score方法,将卡片名称转换为整数,然后从hand数组返回总分 def random_card cards = ["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"] cards[rand(13)] end hand = ["four", "qu
score
方法,将卡片名称转换为整数,然后从hand
数组返回总分
def random_card
cards = ["two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king", "ace"]
cards[rand(13)]
end
hand = ["four", "queen", "ace", "seven"]
def score(hand)
values = {
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5,
"six" => 6,
"seven" => 7,
"eight" => 8,
"nine" => 9,
"ten" => 10,
"jack" => 10,
"queen" => 10,
"king" => 10,
"ace" => 11
}
p hand
final_score = 0
i = 0
while i < hand.length
hand[i] = values[hand[i]]
i += 1
end
hand.each do |card|
final_score += card
end
if final_score <= 21
puts "You scored: " + final_score.to_s
else
puts "You busted with: " + final_score.to_s
end
end
def随机_卡
卡片=[“二”、“三”、“四”、“五”、“六”、“七”,
“八”,“九”,“十”,
“杰克”、“女王”、“国王”、“王牌”]
卡片[rand(13)]
结束
手牌=[“四”、“女王”、“王牌”、“七”]
def分数(手)
值={
“2”=>2,
“三”=>3,
“四”=>4,
“五”=>5,
“六”=>6,
“七”=>7,
“八”=>8,
“九”=>9,
“十”=>10,
“杰克”=>10,
“女王”=>10,
“国王”=>10,
“ace”=>11
}
徒手
最终得分=0
i=0
而我的手
hand[i]=值[hand[i]]
i+=1
结束
每一张手牌|
最终得分+=卡
结束
如果最终得分则该问题称为可变范围。当您访问score方法内部的变量hand
时,您访问的是作为参数传递的局部变量。但您希望访问类变量hand
您可以在访问它时调用self.hand
(我不建议这样做,因为这样会使代码更加难闻),或者重构方法分数以返回分数数组并在方法之外处理它 这个问题称为变量范围。当您访问score方法内部的变量hand
时,您访问的是作为参数传递的局部变量。但您希望访问类变量hand
您可以在访问它时调用self.hand
(我不建议这样做,因为这样会使代码更加难闻),或者重构方法分数以返回分数数组并在方法之外处理它 问题可能来自修改数组-实际上hand
数组是可变的,因此当您在hand[i]=values[hand[i]
行中重新分配值时,您最终会破坏原始的hand。对同一score
方法的后续调用将不再有效
您不应该对其进行变异,而应该对其进行迭代:
VALUES = {
'two' => 2,
...
}
def score(hand)
VALUES.values_at(*hand).reduce(0, &:+)
end
def busted?(score)
score > 21
end
def print_score(score)
puts "You #{busted?(score) ? 'busted with' : 'scored'}: #{score}"
end
def random_card
VALUES.keys.sample
end
hand = Array.new(4) { random_card }
# => ["four", "queen", "ace", "seven"]
your_score = score(hand)
# => 32
print_score(your_score)
# "You busted with: 32"
问题可能来自修改数组-实际上hand
数组是可变的,因此当您在hand[i]=values[hand[i]]
行中重新分配值时,您最终会破坏原始的hand。对同一score
方法的后续调用将不再有效
您不应该对其进行变异,而应该对其进行迭代:
VALUES = {
'two' => 2,
...
}
def score(hand)
VALUES.values_at(*hand).reduce(0, &:+)
end
def busted?(score)
score > 21
end
def print_score(score)
puts "You #{busted?(score) ? 'busted with' : 'scored'}: #{score}"
end
def random_card
VALUES.keys.sample
end
hand = Array.new(4) { random_card }
# => ["four", "queen", "ace", "seven"]
your_score = score(hand)
# => 32
print_score(your_score)
# "You busted with: 32"
发现问题:我没有使用任何参数调用该方法。发现问题:我没有使用任何参数调用该方法。可能是sum
(v2.4中新增)代替reduce(0,&:+)
(可能只是reduce(:+)
(注意:没有&
)。或者,hand.sum{card}值[card]}
reduce
如果数组为空,没有初始累加器将无法工作。(sum
可以很好地工作)虽然reduce(0,:+)
已经足够了。等一下。你怎么能在21点没有牌的情况下有牌?我发现“在这种情况下它不会发生”原因非常错误。即使在这种情况下,你也可以有一个自动计算器来检查所有的手,而将空手作为特例并不是一个好方法。此外,我见过太多没有初始累加器的情况下应用reduce,这是因为程序员没有考虑空手。比sorryThe counte更安全r参数是,如果数组不应为空,则应在其为空时引发异常。可能是sum
(v2.4中新增)代替reduce(0,&:+)
(可以是reduce(:+)
(注意:否&
)。或者,hand.sum{card}值[card]}
reduce
如果数组为空,没有初始累加器将无法工作。(sum
可以很好地工作)虽然reduce(0,:+)
已经足够了。等一下。你怎么能在21点没有牌的情况下有牌?我发现“在这种情况下它不会发生”原因非常错误。即使在这种情况下,你也可以有一个自动计算器来检查所有的手,而将空手作为特例并不是一个好方法。此外,我见过太多没有初始累加器的情况下应用reduce,这是因为程序员没有考虑空手。比sorryThe counte更安全r参数是,如果数组不应为空,则应在数组为空时引发异常。