how to use map in Go

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

how to use map in Go

问题

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

  1. 输入债务金额
  2. 输入分期付款的期限
  3. 前半部分的分期付款银行利息为11%,后半部分为8%
  4. 必须使用映射(maps)

以下是我的代码:

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Print("输入债务金额:")
  5. var debt int
  6. fmt.Scanln(&debt)
  7. fmt.Print("输入分期付款的期限:")
  8. var installment int
  9. fmt.Scanln(&installment)
  10. fmt.Println("====================================================")
  11. fmt.Println("总债务:", debt)
  12. fmt.Println("分期付款期限:", installment)
  13. fmt.Println("====================================================")
  14. var firstHalf = installment / 2
  15. var pay int
  16. for i := 1; i <= installment; i++ {
  17. value := map[string]int{
  18. "月份": i,
  19. "付款金额": pay,
  20. }
  21. if i <= firstHalf {
  22. pay = (debt / installment) + (debt * 11 / 100)
  23. fmt.Println(value["月份"],"带有银行利息(11%)的分期付款金额为", value["付款金额"])
  24. } else {
  25. pay = (debt / installment) + (debt * 8 / 100)
  26. fmt.Println(value["月份"],"带有银行利息(8%)的分期付款金额为", value["付款金额"])
  27. }
  28. }
  29. }

如果我运行这段代码,例如:

债务金额为10,000,000
分期付款期限为7个月

以下是输出结果:

  1. 1 带有银行利息(11%)的分期付款金额为 0
  2. 2 带有银行利息(11%)的分期付款金额为 2528571
  3. 3 带有银行利息(11%)的分期付款金额为 2528571
  4. 4 带有银行利息(8%)的分期付款金额为 2528571
  5. 5 带有银行利息(8%)的分期付款金额为 2228571
  6. 6 带有银行利息(8%)的分期付款金额为 2228571
  7. 7 带有银行利息(8%)的分期付款金额为 2228571
  8. 我不知道为什么第一个索引总是为0,即使后面的计算是正确的。所以,我猜可能是我使用了错误的语法,或者我试图做一些不能完成的事情。也许有经验的人会立即看出问题所在。
  9. <details>
  10. <summary>英文:</summary>
  11. I want to make a simple program to count debt installments. The requirements are:
  12. 1. Input the debt value
  13. 2. Input how long the installments
  14. 3. The first half of the installment bank interest is 11% and the rest are 8%
  15. 4. Must use maps
  16. Here&#39;s my code
  17. package main
  18. import &quot;fmt&quot;
  19. func main() {
  20. fmt.Print(&quot;Input the debt value : &quot;)
  21. var debt int
  22. fmt.Scanln(&amp;debt)
  23. fmt.Print(&quot;Input how long the installments : &quot;)
  24. var installment int
  25. fmt.Scanln(&amp;installment)
  26. fmt.Println(&quot;====================================================&quot;)
  27. fmt.Println(&quot;Total debt : &quot;, debt)
  28. fmt.Println(&quot;Installments : &quot;, installment)
  29. fmt.Println(&quot;====================================================&quot;)
  30. var firstHalf = installment / 2
  31. var pay int
  32. for i := 1; i &lt;= installment; i++ {
  33. value := map[string]int{
  34. &quot;month&quot;: i,
  35. &quot;payment&quot;: pay,
  36. }
  37. if i &lt;= firstHalf {
  38. pay = (debt / installment) + (debt * 11 / 100)
  39. fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (11%) is&quot;, value[&quot;payment&quot;])
  40. } else {
  41. pay = (debt / installment) + (debt * 8 / 100)
  42. fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (8%) is&quot;, value[&quot;payment&quot;])
  43. }
  44. }
  45. }
  46. If I run the code and for example :
  47. The debt is 10.000.000
  48. The installments are 7 months
  49. Here&#39;s the output :
  50. 1 Installment with bank interest (11%) is 0
  51. 2 Installment with bank interest (11%) is 2528571
  52. 3 Installment with bank interest (11%) is 2528571
  53. 4 Installment with bank interest (8%) is 2528571
  54. 5 Installment with bank interest (8%) is 2228571
  55. 6 Installment with bank interest (8%) is 2228571
  56. 7 Installment with bank interest (8%) is 2228571
  57. I don&#39;t know why the first index is always 0, even the next calculation is right. So, I guess that either I am using the 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.
  58. </details>
  59. # 答案1
  60. **得分**: 2
  61. 如果 i <= firstHalf {
  62. pay = (debt / installment) + (debt * 11 / 100)
  63. value["payment"] = pay
  64. fmt.Println(value["month"],"银行利息(11%)的分期付款为",
  65. value["payment"])
  66. } else {
  67. pay = (debt / installment) + (debt * 8 / 100)
  68. value["payment"] = pay
  69. fmt.Println(value["month"],"银行利息(8%)的分期付款为",
  70. value["payment"])
  71. }
  72. <details>
  73. <summary>英文:</summary>
  74. if i &lt;= firstHalf {
  75. pay = (debt / installment) + (debt * 11 / 100)
  76. value[&quot;payment&quot;] = pay
  77. fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (11%) is&quot;,
  78. value[&quot;payment&quot;])
  79. } else {
  80. pay = (debt / installment) + (debt * 8 / 100)
  81. value[&quot;payment&quot;] = pay
  82. fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (8%) is&quot;,
  83. value[&quot;payment&quot;])
  84. }
  85. </details>
  86. # 答案2
  87. **得分**: 1
  88. 它打印出`payment`的值为0,因为它被赋值为`pay`,而`pay`最初没有值。你可以通过在if else条件语句之后声明map,然后打印你的值来修复这个问题,以下是修改后的代码:
  89. ```go
  90. package main
  91. import "fmt"
  92. func main() {
  93. fmt.Print("输入债务金额:")
  94. var debt int
  95. fmt.Scanln(&debt)
  96. fmt.Print("输入分期期限:")
  97. var installment int
  98. fmt.Scanln(&installment)
  99. fmt.Println("====================================================")
  100. fmt.Println("总债务:", debt)
  101. fmt.Println("分期期限:", installment)
  102. fmt.Println("====================================================")
  103. var firstHalf = installment / 2
  104. for i := 1; i <= installment; i++ {
  105. var pay int
  106. if i <= firstHalf {
  107. pay = (debt / installment) + (debt * 11 / 100)
  108. } else {
  109. pay = (debt / installment) + (debt * 8 / 100)
  110. }
  111. value := map[string]int{
  112. "month": i,
  113. "payment": pay,
  114. }
  115. if i <= firstHalf {
  116. fmt.Println(value["month"], "银行利息为11%的分期付款金额为", value["payment"])
  117. } else {
  118. fmt.Println(value["month"], "银行利息为8%的分期付款金额为", value["payment"])
  119. }
  120. }
  121. }

输出:

  1. 输入债务金额:1000
  2. 输入分期期限:5
  3. ====================================================
  4. 总债务: 1000
  5. 分期期限: 5
  6. ====================================================
  7. 1 银行利息为11%的分期付款金额为 310
  8. 2 银行利息为11%的分期付款金额为 310
  9. 3 银行利息为8%的分期付款金额为 280
  10. 4 银行利息为8%的分期付款金额为 280
  11. 5 银行利息为8%的分期付款金额为 280
英文:

It is printing the the payment value of map as 0 because it is assigned with pay which has no value initially.You can fix this by declaring the map beneath the if else condition and then print your values,here is the modified code for the same logic:

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. fmt.Print(&quot;Input the debt value : &quot;)
  5. var debt int
  6. fmt.Scanln(&amp;debt)
  7. fmt.Print(&quot;Input how long the installments : &quot;)
  8. var installment int
  9. fmt.Scanln(&amp;installment)
  10. fmt.Println(&quot;====================================================&quot;)
  11. fmt.Println(&quot;Total debt : &quot;, debt)
  12. fmt.Println(&quot;Installments : &quot;, installment)
  13. fmt.Println(&quot;====================================================&quot;)
  14. var firstHalf = installment / 2
  15. var pay int
  16. for i := 1; i &lt;= installment; i++ {
  17. if i &lt;= firstHalf {
  18. pay = (debt / installment) + (debt * 11 / 100)
  19. } else {
  20. pay = (debt / installment) + (debt * 8 / 100)
  21. }
  22. value := map[string]int{
  23. &quot;month&quot;: i,
  24. &quot;payment&quot;: pay,
  25. }
  26. if i &lt;= firstHalf {
  27. fmt.Println(value[&quot;month&quot;], &quot;Installment with bank interest (11%) is&quot;, value[&quot;payment&quot;])
  28. } else {
  29. fmt.Println(value[&quot;month&quot;], &quot;Installment with bank interest (8%) is&quot;, value[&quot;payment&quot;])
  30. }
  31. }
  32. }

Output:

  1. Input the debt value : 1000
  2. Input how long the installments : 5
  3. ====================================================
  4. Total debt : 1000
  5. Installments : 5
  6. ====================================================
  7. 1 Installment with bank interest (11%) is 310
  8. 2 Installment with bank interest (11%) is 310
  9. 3 Installment with bank interest (8%) is 280
  10. 4 Installment with bank interest (8%) is 280
  11. 5 Installment with bank interest (8%) is 280

huangapple
  • 本文由 发表于 2021年8月10日 10:59:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/68720578.html
匿名

发表评论

匿名网友

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

确定