golang数组的简单应用
作者:互联网
打印A-Z
package main import "fmt" func main() { var arr01 [26]byte for i := 0; i < len(arr01); i++ { arr01[i] = 'A' + byte(i) } for i := 0; i < len(arr01); i++ { fmt.Printf("%c ", arr01[i]) } }
找出数组中的最大值以及它的下标
package main import "fmt" func main() { var num [5]int = [5]int{1, 5, 9, 4, 8} var max int = 0 var maxi int = 0 for i := 0; i < len(num); i++ { if max < num[i] { max = num[i] maxi = i } } fmt.Printf("数组的最大值为 %v, 其下标为 %v", max, maxi) }
求数组的和以及平均值
package main import "fmt" func main() { var myarr [10]int = [10]int{1, 9, 13, 5, 78, 99, 51, 21, 33, 44} var sum int var avgMyarr float64 for _, value := range myarr { sum = sum + value } fmt.Println("数组的和为 ", sum) avgMyarr = float64(sum) / 10 fmt.Println("数组的平均值为 ", avgMyarr) }
标签:数组,int,fmt,arr01,golang,应用,var,main,sum 来源: https://www.cnblogs.com/PatrickStarGazer/p/15938903.html