如何在循环中减少值

huangapple go评论71阅读模式
英文:

how to reduce value in loop

问题

我想制作一个简单的程序来计算为期5年的债务分期付款。要求如下:

  1. 第一年和第二年的银行利息为8%。
  2. 第三年的银行利息为9%。
  3. 第四年和第五年的银行利息为10%。

输出应包括:

  1. 第一年和第二年的分期付款金额。
  2. 第三年之前的剩余债务。
  3. 第三年的分期付款金额。
  4. 第四年之前的剩余债务。
  5. 第四年和第五年的分期付款金额。

以下是你的代码:

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 :

  1. The 1st and 2nd Year the bank interest is 8%
  2. The 3rd year of bank interest is 9%
  3. The 4th and 5th year the bank interest is 10%

and the output should be :

  1. The value of 1st and 2nd installment
  2. remaining debt before 3rd year
  3. the value of 3rd installment
  4. remaining debt before 4th year
  5. the value of 4th and 5th installment

and here's my code :

package main
import &quot;fmt&quot;

func main() {
	var debt = 200000000
	var installment int

	for i := 1; i &lt;= 5; i++ {
		var remainDebt int
		if i &lt; 3 {
			installment = (debt / 5) + (debt * 8 / 100)
			remainDebt = debt - installment
			fmt.Println(&quot;Year&quot;, i, &quot;is&quot;, installment)
			fmt.Println(&quot;Remaining debt is&quot;, remainDebt)
		} else if i == 3 {
			installment = (debt / 5) + (debt * 9 / 100)
			remainDebt = debt - installment
			fmt.Println(&quot;Year&quot;, i, &quot;is&quot;, installment)
			fmt.Println(&quot;Remaining debt is&quot;, remainDebt)

		} else if i &gt; 3 {
			installment = (debt / 5) + (debt * 10 / 100)
			fmt.Println(&quot;Year&quot;, i, &quot;is&quot;, 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&#39;s starting value should be debt value. In your code, each iteration it becomes it&#39;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>



huangapple
  • 本文由 发表于 2021年7月15日 12:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68387983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定