puts "Im gonna ask you for 10 numbers and then give you the sum"
puts "Give me the first number"
first_number= gets.chomp
puts "Give me the second number"
second_number=gets.chomp
puts "Give me the third number "
third_number=gets.chomp
puts "Give me the fourth number"
fourth_number= gets.chomp
puts "Give me the fifth number"
fifth_number=gets.chomp
puts "Give me the sixth number"
sixth_number=gets.chomp
puts "Give me the seventh number"
seventh_number=gets.chomp
puts "Give me the eighth number"
eighth_number=gets.chomp
puts "Give me the ninth number"
ninth_number=gets.chomp
puts "Give me the tenth number"
tenth_number= gets.chomp
puts "The Sum of all your TEN numbers is:"
puts first_number.to_i + second_number.to_i+ third_number.to_i + fourth_number.to_i + fifth_number.to_i + sixth_number.to_i+ seventh_number.to_i+ eighth_number.to_i+ ninth_number.to_i + tenth_number.to_i
答案 0 :(得分:0)
sum = 10.times.inject(0) do|sum,i|
puts "Enter #{i+1}th number"
sum = sum + gets.chomp.to_f
end
puts "The sum is #{sum}"
此示例适用于您传递的十进制和整数。
您错过了一点,您是一名软件编写者。您为最终用户编写软件,并由用户编写,他们想要输入多少个数字。所以我建议你学习编写动态软件。 不要只是对所有内容进行硬编码。使事物变得灵活,以便轻松调整。
一个想法是,您甚至可以询问用户他们想要输入多少个号码。