这是我的代码试图计算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