为了账号安全,请及时绑定邮箱和手机立即绑定

如何在围棋中使用地图

如何在围棋中使用地图

Go
30秒到达战场 2022-09-19 17:27:58
我想做一个简单的程序来计算债务分期付款。要求是:输入债务价值输入分期付款多长时间分期付款银行的上半年利率为11%,其余为8%必须使用地图这是我的代码package mainimport "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"])        }    }}如果我运行代码,例如:债务是10.000.000分期付款是7个月这是输出:1 Installment with bank interest (11%) is 02 Installment with bank interest (11%) is 25285713 Installment with bank interest (11%) is 25285714 Installment with bank interest (8%) is 25285715 Installment with bank interest (8%) is 22285716 Installment with bank interest (8%) is 22285717 Installment with bank interest (8%) is 2228571我不知道为什么第一个索引总是0,甚至下一个计算都是对的。所以,我想要么是我使用了错误的语法,要么是我试图做一些无法做到的事情。也许最有可能有经验的人会立即看到问题所在。
查看完整描述

2 回答

?
慕村225694

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"])

}


查看完整回答
反对 回复 2022-09-19
?
GCT1015

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



查看完整回答
反对 回复 2022-09-19
  • 2 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信