如何在围棋中使用地图

我想做一个简单的程序来计算债务分期付款。要求是:

  1. 输入债务价值

  2. 输入分期付款多长时间

  3. 分期付款银行的上半年利率为11%,其余为8%

  4. 必须使用地图

这是我的代码

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

        }

    }


}

如果我运行代码,例如:


债务是10.000.000分期付款是7个月


这是输出:


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

我不知道为什么第一个索引总是0,甚至下一个计算都是对的。所以,我想要么是我使用了错误的语法,要么是我试图做一些无法做到的事情。也许最有可能有经验的人会立即看到问题所在。


30秒到达战场
浏览 60回答 2
2回答

慕村225694

if i <= firstHalf {&nbsp; &nbsp;pay = (debt / installment) + (debt * 11 / 100)&nbsp; &nbsp;value["payment"] = pay&nbsp; &nbsp;fmt.Println(value["month"],"Installment with bank interest (11%) is",&nbsp;&nbsp; &nbsp;value["payment"])} else {&nbsp; &nbsp; &nbsp;pay = (debt / installment) + (debt * 8 / 100)&nbsp; &nbsp; &nbsp;value["payment"] = pay&nbsp; &nbsp; &nbsp;fmt.Println(value["month"],"Installment with bank interest (8%) is",&nbsp;&nbsp; &nbsp; &nbsp;value["payment"])}

GCT1015

它将 map 的值打印为 0,因为它最初分配没有值。您可以通过在 if else 条件下声明映射来解决此问题,然后打印您的值,下面是相同逻辑的修改代码:paymentpaypackage mainimport "fmt"func main() {&nbsp; &nbsp; fmt.Print("Input the debt value : ")&nbsp; &nbsp; var debt int&nbsp; &nbsp; fmt.Scanln(&debt)&nbsp; &nbsp; fmt.Print("Input how long the installments : ")&nbsp; &nbsp; var installment int&nbsp; &nbsp; fmt.Scanln(&installment)&nbsp; &nbsp; fmt.Println("====================================================")&nbsp; &nbsp; fmt.Println("Total debt : ", debt)&nbsp; &nbsp; fmt.Println("Installments : ", installment)&nbsp; &nbsp; fmt.Println("====================================================")&nbsp; &nbsp; var firstHalf = installment / 2&nbsp; &nbsp; var pay int&nbsp; &nbsp; for i := 1; i <= installment; i++ {&nbsp; &nbsp; &nbsp; &nbsp; if i <= firstHalf {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pay = (debt / installment) + (debt * 11 / 100)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pay = (debt / installment) + (debt * 8 / 100)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; value := map[string]int{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month":&nbsp; &nbsp;i,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "payment": pay,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if i <= firstHalf {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(value["month"], "Installment with bank interest (11%) is", value["payment"])&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(value["month"], "Installment with bank interest (8%) is", value["payment"])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:Input the debt value : 1000Input how long the installments : 5====================================================Total debt :&nbsp; 1000Installments :&nbsp; 5====================================================1 Installment with bank interest (11%) is 3102 Installment with bank interest (11%) is 3103 Installment with bank interest (8%) is 2804 Installment with bank interest (8%) is 2805 Installment with bank interest (8%) is 280
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go