2017-06-07 4 views
-4
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

[ordinalize](https://rubygems.org/gems/ordinalize)のgemを利用することができます。次に、 '' 1.ordinalize#=> first''、 '2.ordinalize#=> second''などを行うことができます。 –

+2

あなたの質問は何ですか? –

+0

プログラムを冗長にする必要がありますか? – Stefan

答えて

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

合計を返すプログラムを作成します。

あなたがソフトウェアライターであるという点は不明です。エンドユーザーとそのユーザーまでのソフトウェア、入力したい番号の数。だから私は動的なソフトウェアを書くことを学ぶことをお勧めします。 すべてをハードコードしないでください。簡単に調整できるように、柔軟なものにする。

1つのアイデアは、入力する数字の数を入力することもできます。

+0

'(1..10)'の 'i {{{i + 1} th} 'は意味がありません。 –

+0

はい、合意しましたが、その場合はありません。 1つは注意する必要があります。 – illusionist

関連する問題