数组应用小案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @Author: Administrator
* @Description:
* @File: 1.数组应用案例
* @Version: 1.13.4
* @Date: 2020/1/22 2:31
*/

package main

import (
"fmt"
"math/rand"
"time"
)

func main01(){

//1)创建一个byte类型的26个元素的数组,分别放置'a'-'z'
//使用for循环访问所有的元素并且打印出来,提示:字符数据运算 'A'+1 ->'b'
var Mychars[26]byte
for i:=0; i< len(Mychars); i++{

Mychars[i] = 'A' + byte(i)
}

for i :=0; i<len(Mychars);i++{

fmt.Printf("%c ",Mychars[i])

}
fmt.Println()
//请求出一个数组的最大值,并得到对应的下表

Arrylist := [...]int{11,22,33,44,55,66,999}
ArryVal := Arrylist[0]
ArryIndex := 0

for i :=0; i<len(Arrylist); i++{

if Arrylist[i] > ArryVal{

ArryVal = Arrylist[i]
ArryIndex = i
}
}

fmt.Printf("ArryVal =:%v ArryIndex =:%v",ArryVal,ArryIndex)

fmt.Println()

//请求出一个数组的平均值,并且同事求出和
NewArry := [...]int{11,22,33,44,55,66,999}
sum := 0
for _,v := range NewArry{

sum += v

}
fmt.Printf("总和:%v\n",sum)
fmt.Printf("平均个值:%v\n",sum/len(NewArry))

//随机生成5个数,放到一个数组 并且将其翻转

randnumber := [5]int{}
rand.Seed(time.Now().UnixNano())

for i := 0; i<len(randnumber); i++{

randnumber[i] = rand.Intn(100)// >=0 <100

}
fmt.Println("交换钱=:",randnumber)
temp :=0
for i :=0; i<len(randnumber)/2; i++{
temp = randnumber[len(randnumber) -1 -i]
randnumber[len(randnumber) -1 -i] = randnumber[i]
randnumber[i] = temp
}
fmt.Println("交换后=:",randnumber)
}