# 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
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....