- # Gets the Average of a list of numbers.
- def Average(array)
- # Placeholder for our answer
- answer = 0
- # Placeholder for our sum
- sum = 0
- # Get the length of the items in the array
- length = array.length
- # Loop through each number in our array
- array.each do |number|
- # Add each number to our current sum
- sum = sum + number.to_i
- # Exit the loop
- end
- # Get the average
- answer = (sum / length)
- # Return the answer
- return (sum / length)
- # Exit the method
- end
- # Gets the Average of a list of numbers.
- def Average2(string)
- # Placeholder for our answer
- answer = 0
- # Placeholder for our sum
- sum = 0
- # Create an array from our list of numbers
- array = string.split(',')
- # Get the length of the items in the array
- length = array.length
- # Loop through each number in our array
- array.each do |number|
- # Add each number to our current sum
- sum = sum + number.to_i
- # Exit the loop
- end
- # Get the average
- answer = (sum / length)
- # Return the answer
- return (sum / length)
- # Exit the method
- end
- #该片段来自于http://www.codesnippet.cn/detail/280420133093.html
来源: http://www.codesnippet.cn/detail/280420133093.html