如何创建一个存储值并将其填充到ruby中的数组?
我是Ruby的noob,我需要一些帮助我的代码。如何创建一个存储值并将其填充到ruby中的数组?
我只是在找到如何创建一个数组来存储值,然后显示它。现在我的代码显示了多少积累的兴趣取决于你一开始就有多少年和储蓄。现在我想让它表明,如果我将5年的时间内显示一个阵列多少年,在这5年内,它会增加兴趣;例如3年[3367.34,3401.01,3435.02(这些值是1%的股权)
我的代码:
puts "How much would you like to save?"
savings = gets.chomp.to_f
puts "For how long would you like to save it for?"
time = gets.chomp.to_i
(savings > 0 || true) && (time > 0 || true)
i = 0.01
counter = 1
until counter > time do
account = Hash.new
savings = (savings + savings * i)
account["Your account"] = savings.to_f.round(2)
counter += 1
end
account.each { |savings| puts "Interest calculated over #{time} years #{savings}" }
新增 我只是说这个部分我的代码,它确实会生成一个数组,但不知道如何调整,以便它可以将用于存储的感兴趣值(在用户需要的年限内)加入到数组中。例如:如果我要放3年,答案看起来应该是这样==> savings = array [3367.34,3401.01,3435.02]每个数额表示当年积累的利息(1styr)= 3367.34(2ndyr)3401.01(3rdyr) 3435.02
> puts "how much will you save?"
> n=gets.to_i
> array= Array(0..n)
> puts array.inspect
这里是如何让储蓄信息到一个数组,但说实话,还是有足够多的其他问题与您的代码,这不会解决所有问题。
array = []
until counter > time do
savings = (savings + savings * i)
array << savings.to_f.round(2) # this pushes the new value onto your array
counter += 1
end
谢谢!我需要知道如何在数组中获取多个数量。例如:如果我要放3年,答案应该看起来像这样==> [“您的账户”=> 3367.34,3401.01,3435.02],每个数额表示当年累计的利息(1styr)= 3367.34(2ndyr)3401.01 (3rdyr)3435.02 @bek – Carol
什么是您的错误 –
没有错误,我只是不知道弄显示各年利息阵列的输出增加储蓄。 @maxpleaner – Carol