package main import "fmt" func main() { //for遍历 arr := [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for i := 0; i < len(arr); i++ { for j := 0; j < len(arr[i]); j++ { fmt.Println(arr[i][j]) } fmt.Println() } //for range遍历 for index, value := range arr { for index1, value1 := range value { fmt.Printf("arr[%v][%v] = %v \t", index, index1, value1) } fmt.Println() } //定义二维数组,用于保存三个班,每个班五名同学成绩,并求出每个班平均分、以及所有班级平均分 var class [3][5]float64 for i := 0; i < len(class); i++ { for j := 0; j < len(class[i]); j++ { fmt.Printf("请输入第%d班的第%d个学生的成绩\n", i+1, j+1) fmt.Scanln(&class[i][j]) } } totalsum := 0.0 for i := 0; i < len(class); i++ { sum := 0.0 for j := 0; j < len(class[i]); j++ { sum = class[i][j] + sum } totalsum = totalsum + sum fmt.Printf("第%d班级的总分为%v, 平均分为%v\n", i+1, sum, sum/float64(len(class[i]))) } fmt.Printf("所有班级的总分为%v, 平均分为%v\n", totalsum, totalsum/3) }