我正在尝试更改特定的坐标,但是数组正在更新所有坐标。
目标是将fixed属性更改为单个坐标。
class Case
attr_accessor :fixed
def initialize
self.fixed = false
end
def fixed?
!!fixed
end
end
def display(arr)
5.times do |x|
5.times do |y|
print arr[x][y].fixed?
print ' '
end
puts
end
end
# Defining array
arr = Array.new(5){ Array.new(5, Case.new) }
# Displaying the arrays
display(arr)
# Changing value of a single coord
arr[2][3].fixed = true
# Displaying the arrays
display(arr)
Run Code Online (Sandbox Code Playgroud)
这是第一个显示呼叫的结果
false false false false false
false false false false false
false false false false …Run Code Online (Sandbox Code Playgroud) 我需要找到一个数组中零子数组的数量:
array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)
结果应该是:3因为我们有0, 0,0和0, 0, 0。
计算零(6)的数量将无效。