当我为任何n写“puts get_rarity(n)”时,为什么这个方法没有返回任何内容?
def get_rarity(float)
if float.between?(1, 0.4)
"common"
elsif float.between?(0.4, 0.2)
"uncommon"
elsif float.between?(0.2, 0.15)
"rare"
elsif float.between?(0.15, 0.14)
"epic"
elsif float.between?(0.14, 0.139)
"legendary"
end
end
答案 0 :(得分:1)
有一个拼写错误puts get_ratity(n)
。它应该是puts get_rarity(n)
。
将代码写为:
def get_rarity(float)
if float.between?(0.4, 1) # see the change..(min, max)
"common"
elsif float.between?(0.2, 0.4) # see the change..(min, max)
"uncommon"
elsif float.between?(0.15, 0.2) # see the change..(min, max)
"rare"
elsif float.between?(0.14, 0.15) # see the change..(min, max)
"epic"
elsif float.between?(0.139, 0.14) # see the change..(min, max)
"legendary"
end
end
阅读文档between?(min, max)
。这意味着方法#between?
的第一个参数应该是 min ,第二个参数应该是 max 。
答案 1 :(得分:1)
您确定 float 始终在1..0.139范围内吗?
在调用between?
时首先放置较小的值。
试试这个:
def get_rarity(float)
if float.between?(0.4, 1)
"common"
elsif float.between?(0.2, 0.4)
"uncommon"
elsif float.between?(0.15, 0.2)
"rare"
elsif float.between?(0.14, 0.15)
"epic"
elsif float.between?(0.139, 0.14)
"legendary"
else
"undefined"
end
end