英文:
how to reduce value in loop
问题
我想制作一个简单的程序来计算为期5年的债务分期付款。要求如下:
- 第一年和第二年的银行利息为8%。
- 第三年的银行利息为9%。
- 第四年和第五年的银行利息为10%。
输出应包括:
- 第一年和第二年的分期付款金额。
- 第三年之前的剩余债务。
- 第三年的分期付款金额。
- 第四年之前的剩余债务。
- 第四年和第五年的分期付款金额。
以下是你的代码:
package main
import "fmt"
func main() {
var debt = 200000000
var installment int
for i := 1; i <= 5; i++ {
var remainDebt int
if i < 3 {
installment = (debt / 5) + (debt * 8 / 100)
remainDebt = debt - installment
fmt.Println("第", i, "年的分期付款金额为", installment)
fmt.Println("剩余债务为", remainDebt)
} else if i == 3 {
installment = (debt / 5) + (debt * 9 / 100)
remainDebt = debt - installment
fmt.Println("第", i, "年的分期付款金额为", installment)
fmt.Println("剩余债务为", remainDebt)
} else if i > 3 {
installment = (debt / 5) + (debt * 10 / 100)
fmt.Println("第", i, "年的分期付款金额为", installment)
}
}
}
我的代码输出结果为:
第1年的分期付款金额为 56000000
剩余债务为 144000000
第2年的分期付款金额为 56000000
剩余债务为 144000000
第3年的分期付款金额为 58000000
剩余债务为 142000000
第4年的分期付款金额为 60000000
第5年的分期付款金额为 60000000
我不知道为什么每年的剩余债务都是错误的。所以,我猜可能是我使用了错误的语法,或者我试图做一些不能完成的事情。也许有经验的人会立即看出问题所在。
英文:
I want to make simple program to count debt installment for 5 years installment.
The requirements is :
- The 1st and 2nd Year the bank interest is 8%
- The 3rd year of bank interest is 9%
- The 4th and 5th year the bank interest is 10%
and the output should be :
- The value of 1st and 2nd installment
- remaining debt before 3rd year
- the value of 3rd installment
- remaining debt before 4th year
- the value of 4th and 5th installment
and here's my code :
package main
import "fmt"
func main() {
var debt = 200000000
var installment int
for i := 1; i <= 5; i++ {
var remainDebt int
if i < 3 {
installment = (debt / 5) + (debt * 8 / 100)
remainDebt = debt - installment
fmt.Println("Year", i, "is", installment)
fmt.Println("Remaining debt is", remainDebt)
} else if i == 3 {
installment = (debt / 5) + (debt * 9 / 100)
remainDebt = debt - installment
fmt.Println("Year", i, "is", installment)
fmt.Println("Remaining debt is", remainDebt)
} else if i > 3 {
installment = (debt / 5) + (debt * 10 / 100)
fmt.Println("Year", i, "is", installment)
}
}
}
And the output from my code is :
Year 1 is 56000000
Remaining debt is 144000000
Year 2 is 56000000
Remaining debt is 144000000
Year 3 is 58000000
Remaining debt is 142000000
Year 4 is 60000000
Year 5 is 60000000
I don't know why the remaining debt from each year are wrong. So, I guess that either I am using wrong syntax or I am trying to do something that can not be done. Maybe most likely experienced people will see right away what is wrong.
答案1
得分: 1
var remainDebt int // 这一行应该在循环之前,并且其初始值应该是债务的值。在你的代码中,每次迭代它都变成了它的默认值0。但是它应该在每次循环迭代中不断减少。
而且,行`remainDebt = debt - instalment`应该更正如下。因为你应该减去你支付的贷款本金金额,而不是带有利息的分期付款。
remainDebt = remainDebt - debt / 5
你可以在这里看到更正后的代码。
<details>
<summary>英文:</summary>
`var remainDebt int` this line should be before the for loop and it's starting value should be debt value. In your code, each iteration it becomes it's default value, 0. But it should be keep reducing in each iteration of your loop.
And line `remainDebt = debt - instalment` should be corrected as below. Because you should reduce Capital amount you paid of the loan, not the instalment with interest.
remainDebt = remainDebt - debt / 5
You can see corrected code [here][1]
[1]: https://play.golang.org/p/4nlROmpHlLz
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论