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

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

@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}"