最佳答案
我试图使增长率(Double
)的计算器,将结果舍入到最近的整数,并从那里重新计算,如下所示:
let firstUsers = 10.0
let growth = 0.1
var users = firstUsers
var week = 0
while users < 14 {
println("week \(week) has \(users) users")
users += users * growth
week += 1
}
但到目前为止我还不能。
< em >编辑 我是这样做的:
var firstUsers = 10.0
let growth = 0.1
var users:Int = Int(firstUsers)
var week = 0
while users <= 14 {
println("week \(week) has \(users) users")
firstUsers += firstUsers * growth
users = Int(firstUsers)
week += 1
}
虽然我不介意它总是四舍五入,但我不喜欢它,因为firstUsers
必须成为一个变量,并在整个程序中改变(为了进行下一次计算),这是我不希望发生的。