英文:
how to use map in Go
问题
我想制作一个简单的程序来计算债务分期付款。要求如下:
- 输入债务金额
- 输入分期付款的期限
- 前半部分的分期付款银行利息为11%,后半部分为8%
- 必须使用映射(maps)
以下是我的代码:
package main
import "fmt"
func main() {
fmt.Print("输入债务金额:")
var debt int
fmt.Scanln(&debt)
fmt.Print("输入分期付款的期限:")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("总债务:", debt)
fmt.Println("分期付款期限:", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
var pay int
for i := 1; i <= installment; i++ {
value := map[string]int{
"月份": i,
"付款金额": pay,
}
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
fmt.Println(value["月份"],"带有银行利息(11%)的分期付款金额为", value["付款金额"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
fmt.Println(value["月份"],"带有银行利息(8%)的分期付款金额为", value["付款金额"])
}
}
}
如果我运行这段代码,例如:
债务金额为10,000,000
分期付款期限为7个月
以下是输出结果:
1 带有银行利息(11%)的分期付款金额为 0
2 带有银行利息(11%)的分期付款金额为 2528571
3 带有银行利息(11%)的分期付款金额为 2528571
4 带有银行利息(8%)的分期付款金额为 2528571
5 带有银行利息(8%)的分期付款金额为 2228571
6 带有银行利息(8%)的分期付款金额为 2228571
7 带有银行利息(8%)的分期付款金额为 2228571
我不知道为什么第一个索引总是为0,即使后面的计算是正确的。所以,我猜可能是我使用了错误的语法,或者我试图做一些不能完成的事情。也许有经验的人会立即看出问题所在。
<details>
<summary>英文:</summary>
I want to make a simple program to count debt installments. The requirements are:
1. Input the debt value
2. Input how long the installments
3. The first half of the installment bank interest is 11% and the rest are 8%
4. Must use maps
Here's my code
package main
import "fmt"
func main() {
fmt.Print("Input the debt value : ")
var debt int
fmt.Scanln(&debt)
fmt.Print("Input how long the installments : ")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("Total debt : ", debt)
fmt.Println("Installments : ", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
var pay int
for i := 1; i <= installment; i++ {
value := map[string]int{
"month": i,
"payment": pay,
}
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
fmt.Println(value["month"],"Installment with bank interest (11%) is", value["payment"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
fmt.Println(value["month"],"Installment with bank interest (8%) is", value["payment"])
}
}
}
If I run the code and for example :
The debt is 10.000.000
The installments are 7 months
Here's the output :
1 Installment with bank interest (11%) is 0
2 Installment with bank interest (11%) is 2528571
3 Installment with bank interest (11%) is 2528571
4 Installment with bank interest (8%) is 2528571
5 Installment with bank interest (8%) is 2228571
6 Installment with bank interest (8%) is 2228571
7 Installment with bank interest (8%) is 2228571
I don'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.
</details>
# 答案1
**得分**: 2
如果 i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
value["payment"] = pay
fmt.Println(value["month"],"银行利息(11%)的分期付款为",
value["payment"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
value["payment"] = pay
fmt.Println(value["month"],"银行利息(8%)的分期付款为",
value["payment"])
}
<details>
<summary>英文:</summary>
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
value["payment"] = pay
fmt.Println(value["month"],"Installment with bank interest (11%) is",
value["payment"])
} else {
pay = (debt / installment) + (debt * 8 / 100)
value["payment"] = pay
fmt.Println(value["month"],"Installment with bank interest (8%) is",
value["payment"])
}
</details>
# 答案2
**得分**: 1
它打印出`payment`的值为0,因为它被赋值为`pay`,而`pay`最初没有值。你可以通过在if else条件语句之后声明map,然后打印你的值来修复这个问题,以下是修改后的代码:
```go
package main
import "fmt"
func main() {
fmt.Print("输入债务金额:")
var debt int
fmt.Scanln(&debt)
fmt.Print("输入分期期限:")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("总债务:", debt)
fmt.Println("分期期限:", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
for i := 1; i <= installment; i++ {
var pay int
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
} else {
pay = (debt / installment) + (debt * 8 / 100)
}
value := map[string]int{
"month": i,
"payment": pay,
}
if i <= firstHalf {
fmt.Println(value["month"], "银行利息为11%的分期付款金额为", value["payment"])
} else {
fmt.Println(value["month"], "银行利息为8%的分期付款金额为", value["payment"])
}
}
}
输出:
输入债务金额:1000
输入分期期限:5
====================================================
总债务: 1000
分期期限: 5
====================================================
1 银行利息为11%的分期付款金额为 310
2 银行利息为11%的分期付款金额为 310
3 银行利息为8%的分期付款金额为 280
4 银行利息为8%的分期付款金额为 280
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:
package main
import "fmt"
func main() {
fmt.Print("Input the debt value : ")
var debt int
fmt.Scanln(&debt)
fmt.Print("Input how long the installments : ")
var installment int
fmt.Scanln(&installment)
fmt.Println("====================================================")
fmt.Println("Total debt : ", debt)
fmt.Println("Installments : ", installment)
fmt.Println("====================================================")
var firstHalf = installment / 2
var pay int
for i := 1; i <= installment; i++ {
if i <= firstHalf {
pay = (debt / installment) + (debt * 11 / 100)
} else {
pay = (debt / installment) + (debt * 8 / 100)
}
value := map[string]int{
"month": i,
"payment": pay,
}
if i <= firstHalf {
fmt.Println(value["month"], "Installment with bank interest (11%) is", value["payment"])
} else {
fmt.Println(value["month"], "Installment with bank interest (8%) is", value["payment"])
}
}
}
Output:
Input the debt value : 1000
Input how long the installments : 5
====================================================
Total debt : 1000
Installments : 5
====================================================
1 Installment with bank interest (11%) is 310
2 Installment with bank interest (11%) is 310
3 Installment with bank interest (8%) is 280
4 Installment with bank interest (8%) is 280
5 Installment with bank interest (8%) is 280
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论