要求:
随机生成数组内容,并将数组反转打印出来
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
//var arr = [...]int{1, 5, 78}
var arr [8]int
// 给数组随机赋值
_len := len(arr)
rand.Seed(time.Now().UnixNano())
for i := 0; i < _len; i++ {
arr[i] = rand.Intn(100)
}
fmt.Println("之前的数组 ", arr)
length := len(arr)
for i := 0; i < length/2; i++ {
temp := arr[length-1-i]
arr[length-1-i] = arr[i]
arr[i] = temp
}
fmt.Println("反转后的数组为 ", arr)
}
结果: