package main import "fmt" func main() { scores := [6]int{95, 91, 39, 60, 21, 100} // 使用数组存储多个学生的成绩 sum := 0 fmt.Printf("数组长度是%v\n", len(scores)) fmt.Printf("数组指针内存地址是%p\n", &scores) fmt.Printf("第一个值内存地址是%p\n", &scores[0]) fmt.Printf("第二个值内存地址是%p\n", &scores[1]) fmt.Printf("第三个值内存地址是%p\n", &scores[2]) fmt.Printf("第三个值内存地址是%p\n", &scores[3]) fmt.Printf("第三个值内存地址是%p\n", &scores[4]) fmt.Printf("第三个值内存地址是%p\n", &scores[5]) // 计算总成绩 for _, score := range scores { sum += score } // 计算平均成绩 avg := float64(sum) / float64(len(scores)) // 使用 len(scores) 获取学生人数 fmt.Printf("总成绩是%v\n平均成绩是%.2f\n", sum, avg) } |
输出结果是
组长度是6 数组指针内存地址是0xc00013a060 第一个值内存地址是0xc00013a060 第二个值内存地址是0xc00013a068 第三个值内存地址是0xc00013a070 第三个值内存地址是0xc00013a078 第三个值内存地址是0xc00013a080 第三个值内存地址是0xc00013a088 总成绩是406 平均成绩是67.67 |