我一直对自己说,一定有更好的方法,但我现在看不到它......想法?
i = 0; lose = 0; win = 0
while i < @array.size
results = @array[i].results
q = 0
while q < results.size
if results[q].to_i == 0 then
lose += 1
elsif results[q].to_i == 1 then
win += 1
else
puts results[q]
puts "false"
end
q += 1
end
i+=1
end
if win == lose then
puts "true"
else
puts "false"
end
Run Code Online (Sandbox Code Playgroud)
您可以使用array.each而不是while循环.
您可以使用array.count而不是手动检查每个数组:
lose = results.count { |r| r.to_i == 0 }
win = results.count { |r| r.to_i == 1 }
# or possibly if the array can only contain wins and losses
win = results.count - lose
Run Code Online (Sandbox Code Playgroud)