how to use map in Go

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

how to use map in Go

问题

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

  1. 输入债务金额
  2. 输入分期付款的期限
  3. 前半部分的分期付款银行利息为11%,后半部分为8%
  4. 必须使用映射(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&#39;s my code 

    package main
    
    import &quot;fmt&quot;
    
    func main() {
    
    	fmt.Print(&quot;Input the debt value : &quot;)
    	var debt int
    	fmt.Scanln(&amp;debt)
    
    	fmt.Print(&quot;Input how long the installments : &quot;)
    	var installment int
    	fmt.Scanln(&amp;installment)
    
    	fmt.Println(&quot;====================================================&quot;)
    	fmt.Println(&quot;Total debt : &quot;, debt)
    	fmt.Println(&quot;Installments : &quot;, installment)
    	fmt.Println(&quot;====================================================&quot;)
    	var firstHalf = installment / 2
    
    	var pay int
    	for i := 1; i &lt;= installment; i++ {
    
    		value := map[string]int{
    			&quot;month&quot;:    i,
    			&quot;payment&quot;: pay,
    		}
    
    		if i &lt;= firstHalf {
    			pay = (debt / installment) + (debt * 11 / 100)
    			fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (11%) is&quot;, value[&quot;payment&quot;])
    		} else {
    			pay = (debt / installment) + (debt * 8 / 100)
    			fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (8%) is&quot;, value[&quot;payment&quot;])
    		}
    	}
    
    }

If I run the code and for example :

The debt is 10.000.000
The installments are 7 months

Here&#39;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&#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.

</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 &lt;= firstHalf {
       pay = (debt / installment) + (debt * 11 / 100)
       value[&quot;payment&quot;] = pay
       fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (11%) is&quot;, 
       value[&quot;payment&quot;])
    } else {
         pay = (debt / installment) + (debt * 8 / 100)
         value[&quot;payment&quot;] = pay
         fmt.Println(value[&quot;month&quot;],&quot;Installment with bank interest (8%) is&quot;, 
         value[&quot;payment&quot;])
    }

</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 &quot;fmt&quot;

func main() {

	fmt.Print(&quot;Input the debt value : &quot;)
	var debt int
	fmt.Scanln(&amp;debt)

	fmt.Print(&quot;Input how long the installments : &quot;)
	var installment int
	fmt.Scanln(&amp;installment)

	fmt.Println(&quot;====================================================&quot;)
	fmt.Println(&quot;Total debt : &quot;, debt)
	fmt.Println(&quot;Installments : &quot;, installment)
	fmt.Println(&quot;====================================================&quot;)
	var firstHalf = installment / 2

	var pay int
	for i := 1; i &lt;= installment; i++ {

		if i &lt;= firstHalf {
			pay = (debt / installment) + (debt * 11 / 100)

		} else {
			pay = (debt / installment) + (debt * 8 / 100)

		}
		value := map[string]int{
			&quot;month&quot;:   i,
			&quot;payment&quot;: pay,
		}

		if i &lt;= firstHalf {

			fmt.Println(value[&quot;month&quot;], &quot;Installment with bank interest (11%) is&quot;, value[&quot;payment&quot;])
		} else {

			fmt.Println(value[&quot;month&quot;], &quot;Installment with bank interest (8%) is&quot;, value[&quot;payment&quot;])
		}

	}

}

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

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:

确定