VB随机产生10个任意的两位正整数存放在一维数组中

求数组的最大值、平均值、能实现将数据升序排列,并且使用InputBox函数插入一个新数据,使数组仍然按升序排列,结果显示在图片框中
Option Base 1
Option Explicit
Dim a(11) As Integer

Private Sub Command1_Click()
Dim i As Integer
picture1.Cls
picture1.Print "产生的一维数组为:"
Randomize
For i = 1 To 10
a(i) = ine(Rnd * 20 + 10)
picture1.Print a(i);
Next i
picture1.Print
End Sub

Private Sub Command2_Click()
Dim max As Integer, p As Integer, i As Integer
max = a(1)

End Sub

Private Sub Command5_Click()
Dim x%, p%, i%
x = Val(InputBox("输入要插入的数据:"))
p = 1
Do While x > a(p) And p <= 10
p = p + 1
Loop
For i = 10 To p Step -1
a(i + 1) = a(i)
Next i
a(p) = x
picture1.Print
picture1.Print "插入后的一维数组为:"
For i = 1 To 11
picture1.Print a(i);
Next i
End Sub

第1个回答  推荐于2017-05-30
Option Base 1
Option Explicit
Dim a(11) As Integer

Private Sub Command1_Click()
Dim i As Integer
Picture1.Cls
Picture1.Print "产生的一维数组为:"
Randomize
For i = 1 To 10
a(i) = Int(Rnd * 20 + 10)
Picture1.Print a(i);
Next i
Picture1.Print
End Sub

Private Sub Command2_Click()
Dim max As Integer, p As Integer, i As Integer
For i = 1 To 10
If max < a(i) Then max = a(i)
Next
Picture1.Print "最大值为:" & max
End Sub

Private Sub Command3_Click()
Dim p As Integer, i As Integer, sum As Integer
For i = 1 To 10
sum = sum + a(i)
Next
Picture1.Print "平均值为:" & CStr(sum / 10)
End Sub

Private Sub Command4_Click()
Dim i As Integer, m As Integer, b(10) As Integer, j As Integer

For i = 1 To 10
b(i) = a(i)
Next i
For i = 10 To 1 Step -1
For j = 1 To i - 1
If b(j) > b(j + 1) Then
m = b(j + 1)
b(j + 1) = b(j)
b(j) = m
End If
Next j
Next i
Picture1.Print "数组按从大到小排序:"
For i = 1 To 10
Picture1.Print b(i);
Next
End Sub

Private Sub Command5_Click()
Dim x%, p%, i%
x = Val(InputBox("输入要插入的数据:"))
p = 1
Do While x > a(p) And p <= 10
p = p + 1
Loop
For i = 10 To p Step -1
a(i + 1) = a(i)
Next i
a(p) = x
Picture1.Print
Picture1.Print "插入后的一维数组为:"
For i = 1 To 11
Picture1.Print a(i);
Next i
End Sub本回答被提问者采纳
相似回答