我想减去我的数组中最大和最小的两个值。我已经排序他们然后试图减去他们,但输出是不同的。我也试图使用循环,但我不断收到错误。很高兴告诉我我做错了什么。感谢红宝石数组与减法
numbers = [randomNo = rand(50),randomNo = rand(50),randomNo = rand(50),randomNo = rand(50)]
loop do
puts "type s to start and q to quit"
input = STDIN.gets
break if input == q
puts numbers.sort.inspect
puts numbers.first - numbers.last
end
output:[3, 13, 21, 46]
18
我想46 - 3
相反只是说的 “它不工作”,试试描述症状,并且'数字= 4.times.collect {rand(50)}'。 – 2012-03-24 20:45:42
对于初学者来说,'input =='q''因为'input'是一个字符串。 – 2012-03-24 20:46:26
感谢您的帮助 – user1239333 2012-03-24 20:55:02