Golang 中“n”个切片值的总和

我正在尝试使用 Slices 和 For Loops 在 golang 中制作一个简单的平均计算器。


但是我在 VS 代码中遇到错误,这个错误:


恐慌:运行时错误:索引超出范围 [0],长度为 0 goroutine 1 [正在运行]:main.main() C:/Desktop/cs50/week2/myarray.go:16 +0x134 退出状态 2


我在 W10 上使用 VS 代码。


我的代码:


package main


import "fmt"


func main() {

    var n int

    scores := []uint{}

    var sumScores float32 = 0


    fmt.Println("How many scores?") //ask user how many values

    fmt.Scan(&n)                    //get how many values


    for i := 0; i < n; i++ {

        fmt.Printf("Scores: ")                     // ask for values

        fmt.Scan(&scores[i])                       // get values

        sumScores = sumScores + float32(scores[i]) // sum values

    }


    fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value

}

知道有什么问题吗?


我认为这可能与我使用 float32 的 Slice 定义有关。


提前谢谢你。


HUWWW
浏览 243回答 2
2回答

白猪掌柜的

这非常有效,您只需指出切片的初始大小:func main() {&nbsp; &nbsp; var n int&nbsp; &nbsp; var sumScores float32 = 0&nbsp; &nbsp; fmt.Println("How many scores?") //ask user how many values&nbsp; &nbsp; fmt.Scan(&n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get how many values&nbsp; &nbsp; scores := make([]uint, n, n)&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Scores: ") // ask for values&nbsp; &nbsp; &nbsp; &nbsp; fmt.Scan(&scores[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// get values&nbsp; &nbsp; &nbsp; &nbsp; sumScores = sumScores + float32(scores[i]) // sum values&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value}为了学习目的您声明scores切片的方式,它只是一个空切片,因此您可以先附加到它,然后扫描新生成位置中的数字。(但这肯定不是解决这个特定问题的方法)func main() {&nbsp; &nbsp; var n int&nbsp; &nbsp; scores := []uint{}&nbsp; &nbsp; var sumScores float32 = 0&nbsp; &nbsp; fmt.Println("How many scores?") //ask user how many values&nbsp; &nbsp; fmt.Scan(&n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get how many values&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Scores: ") // ask for values&nbsp; &nbsp; &nbsp; &nbsp; scores = append(scores, 0)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Scan(&scores[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// get values&nbsp; &nbsp; &nbsp; &nbsp; sumScores = sumScores + float32(scores[i]) // sum values&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value}

FFIVE

分数 := []uint{}这是一个切片字面量,你应该使用 Golang 内置函数append来处理它。就像 @no0ob 的第二个例子一样,或者这个:func main() {&nbsp; &nbsp; var n int&nbsp; &nbsp; var tmpVal uint&nbsp; &nbsp; scores := []uint{}&nbsp; &nbsp; var sumScores float32 = 0&nbsp; &nbsp; fmt.Println("How many scores?") //ask user how many values&nbsp; &nbsp; fmt.Scan(&n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get how many values&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Scores: ") // ask for values&nbsp; &nbsp; &nbsp; &nbsp; fmt.Scan(&tmpVal) // save input to tmpVal&nbsp; &nbsp; &nbsp; &nbsp; scores = append(scores, tmpVal) // append tmpVal to scores&nbsp; &nbsp; &nbsp; &nbsp; sumScores = sumScores + float32(scores[i]) // sum values&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go