我想将迭代中的当前元素与数组中其余元素进行比较。从起点看,我没有任何问题。这个问题出现在我想要比较当前元素与后面的元素之内。比较当前元素和剩余的数组(Ruby)
array = [1, 2, 3, 2, 3, 4, 5]
array.each_with_index do |num, index|
break if array[index + 1] == nil
if num > array[index + 1]
puts "#{num} is greater than the #{array[index + 1]}!"
else
puts "#{num} is less than the #{array[index + 1]}!"
end
end
我要寻找类似:
"3 is greater than 1 and 2 but less than 4 and 5"
任何想法?
剩余*(前意后)。好的赶上(我更新了标题) – user3007294