2 回答
TA贡献1880条经验 获得超4个赞
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"])
}
TA贡献1827条经验 获得超4个赞
它将 map 的值打印为 0,因为它最初分配没有值。您可以通过在 if else 条件下声明映射来解决此问题,然后打印您的值,下面是相同逻辑的修改代码:paymentpay
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"])
}
}
}
输出:
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
- 2 回答
- 0 关注
- 59 浏览
添加回答
举报