作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Shoes 在 Ruby 中制作 Yahtzee 游戏当我点击按钮“Two”时,代码应该计算值 2 出现在数组中。对于出现的值 2 的每个实例,分数增加 2。
此代码适用于特定数量的案例,但适用于其他情况,例如@array = [2,1,2,2,3] # 数组中有三个 2 所以分数应该是 6,但我的代码却返回 4...为什么?
button " twos " do
@array.each_with_index do |value, index|
if (@array[index] == 2)
@score = @score + 2
@points = @score + 2
end #if
end #loop end #button
最佳答案
这段代码看起来更好,但实际上,它做的是同样的事情。也许您应该检查实例变量 @score
和 @points
的初始值?
@array = [2,1,2,2,3]
@score = @points = 0
@score = @array.count(2) * 2
@points = @score
@score
=> 6
@points
=> 6
关于 ruby 鞋子 : counting the amount of times a value occurs in an Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570581/
我是一名优秀的程序员,十分优秀!