package main import "fmt" func main() { /* 数组长度为 5 */ var balance = []int {100, 1, 2, 7, 4} var avg float32 var sum int /* 数组作为参数传递给函数 */ avg = getAverage( balance, 5 ) ; sum = getSum(balance,5); /* 输出返回的平均值 */ fmt.Printf( "平均值为: %.2f \n", avg ); fmt.Println("和为:%d",sum); } func getAverage(arr []int, size int) float32 { var i,sum int var avg float32 for i = 0; i < size;i++ { sum += arr[i] } avg = float32(sum / size) return avg ; } /*和计算公式*/ func getSum(arr []int,size int) int{ var i,sum int for i = 0; i < size; i++{ fmt.Println(arr[i]) sum += arr[i] } return sum }
本文出自 “欢迎评论,欢迎点赞” 博客,请务必保留此出处http://swq499809608.blog.51cto.com/797714/1920886
原文:http://swq499809608.blog.51cto.com/797714/1920886