【发布时间】:2016-04-13 18:52:45
【问题描述】:
这可能是超级基本的,但我已经尝试了足够多的东西但未能达到..
我想把一个数字改成负数。
answer = []
array = [3, 5, 2, 19, 2, 1]
array.each.with_index do |x, i|
if x > array[i+1]
answer << array[i] * -1
else x =< array[i+1]
answer << array[i]
end
end
=> the answer I want is [-5] for when 'true' but I'm getting [5]
我还尝试制作一个新的“negarray”,将所有等效的负数作为“array”
answer = []
array = [3, 5, 2, 19, 2, 1]
negarray = [-3, -5, -2, -19, -2, -1]
=> again, getting [5], and not the [-5] I want.
干杯!
【问题讨论】:
-
无法重现。获取
[-5]。 -
我很抱歉,但我不明白这个问题
-
@ndn - 当我得到 [5] 并三次检查我的代码时,这很奇怪,将其交给其他人运行,他们得到 [-5]。
标签: ruby integer negative-number negative-integer