七叶笔记 » golang编程 » golang数组练习2

golang数组练习2

 package main

import (
"fmt"
)

// SIZE 定义数组大小
const SIZE = 5

func main() {
arr1 := [SIZE]int{11, 23, 45, 66, 89}
arr2 := [SIZE + 1]int{}
num := 0

fmt.Println("arr1 = ", arr1)
fmt.Println("请输入一个待插入的整数")
fmt.Scanf("%d", #)
count := 0
for i := 0; i < SIZE; i++ {

if arr1[i] < num {
arr2[i] = arr1[i]
} else if arr1[i] >= num {
arr2[i] = num
for j := i; j < SIZE; j++ {
arr2[j+1] = arr1[j]
count++
}
count++
break
}
count++

}
if count == SIZE {
arr2[SIZE] = num
}

fmt.Println("arr2 = ", arr2)
}  

运行效果如下:

相关文章