【发布时间】:2014-06-16 22:59:51
【问题描述】:
# Methods for calculating
# print out the input that the user entered
def PrintScores(*numbers)
numbers.each {|x| print x.join(" ")}
puts
end
#print out the scores in ascending order
def ListScores(*numbers)
numbers.sort!
print numbers
end
# Main function
out_file = File.new("out.txt", "w")
puts "Enter the scores you wish to have our stats program look into? "
user_input = gets.chomp
input_array = user_input.split(" ")
input_array.map! do |x|
x.to_i
end
PrintScores(input_array)
ListScores(input_array)
ListScores 函数仍然按照我输入的顺序打印数组,我不知道为什么。
【问题讨论】:
-
你应该使用
ListScores(numbers)而不是ListScores(*numbers) -
不要创建放置或打印的方法。因为 ruby 有一种放置和打印的方法。
标签: ruby arrays sorting methods