小编use*_*983的帖子

Ruby Shoes:计算数组中值发生的次数

当我点击Button"Two"时,我正在Ruby中使用Shoes制作一个Yahtzee游戏,代码假定计算值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
Run Code Online (Sandbox Code Playgroud)

ruby arrays indexing shoes duplicates

3
推荐指数
1
解决办法
2358
查看次数

标签 统计

arrays ×1

duplicates ×1

indexing ×1

ruby ×1

shoes ×1