Ruby计算加权平均分(基于Hash)

2015-06-11 0 160
Ruby计算加权平均分(基于Hash)
@items=Hash.new
puts "please input the number of your courses:"
course_number=gets
puts "please input your score and the balance"
puts "---------------------------------------"
for i in 1..course_number.to_i
  puts "input your score:"
  score=gets
  puts "input its balance:"
  balance=gets
  @items[score]=balance
end
sum=0
total_credit=0
@items.each_pair do |key,value|
  sum=sum+key.to_i*value.to_i
end

@items.each_value { |value|
  total_credit=total_credit+value.to_i
}
result=sum/total_credit
puts "-------------------"
puts "your balance average score is #{result}"



遇见资源网 ruby Ruby计算加权平均分(基于Hash) http://www.ox520.com/16387.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务