答案 0 :(得分:3)
你说的是余额=贷款 - 回报,但你没有修改贷款或回报的价值。我想你想这样做:
balance = loan
for i in (0..<12) {
balance -= payback
print(balance)
}
首先,我们指定平衡贷款的价值,我们减去投资回报,获得预期的效果,并且仍然保持贷款的初始值
答案 1 :(得分:3)
您不会考虑索引变量i
如果loan
是初始值且payback
为300,那么您必须计算loan - payback * i
let loan = 5000
let payback = 300
for i in 0..<17 {
let balance = loan - payback * i
print(balance)
}
答案 2 :(得分:3)
您必须修改贷款价值,如下所示
balance = loan
for i in (0..<12) {
balance = loan - payback
loan = balance
print(balance)
}
答案 3 :(得分:1)
另一种方法:从loan
开始并重复减去payback
,直到数字达到零:
let loan = 5000
let payback = 300
for balance in sequence(first: loan, next: { $0 - payback} ).prefix(while: { $0 > 0 }) {
print(balance)
}
参考文献:
答案 4 :(得分:0)
你可以尝试这个:
var aValue = 5000
let bValue = 300
for i in 0..<12 { //I guess the loop will be max 17 times if value is 5000 or the value will come in Negative
let valueToMinus = bValue * i
let cValue = aValue - valueToMinus
print(cValue)//Resulted Output From Main
}
希望得到这个帮助。