這是我的代碼試圖計算coumpoung興趣如何使用用戶輸入進行算術運算?
def coumpoundinterest
print "Enter the current balance: "
current_balance = gets
print "Enter the interest rate: "
interest_rate = gets
_year = 2012
i = 0
while i < 5 do
current_balance = current_balance + current_balance * interest_rate
puts "The balance in year " + (_year + i).to_s + " is $" + current_balance.to_s
i = i + 1
end
end
這是我得到的一切煩惱
current_balance = current_balance + current_balance * interest_rate
行了。如果我把它的代碼是這樣的,我得到字符串不能強制進入FixNum的錯誤。如果我在interest_rate後添加.to_i,那麼我會多次乘上該行。我如何處理紅寶石中的算術?
使用'to_i'將會消除十進制值。 '「0.05 \ n」.to_i => 0'。應該使用'to_f'。 – oldergod
@oldergod沒錯。我只是按照問題的方法。最好使用'to_f'。 – halfelf
@halfelf,有道理。我仍然試圖用Ruby來思考(不是用C#)。不容易。非常感謝你。 – Richard77