利用VB如何设计下面问题:利用随机数产生10个1~100之间的随机数,显示出最大值,最小值及平均值。
1个回答
展开全部
Private Sub Form_click()
Dim a(9)
Dim m(), n() 'm()为偶数集, n()为奇数集
Dim i, p, q
Dim sum
Dim avg1, avg2 'avg1为奇数平均数, avg2为偶数平均数
Dim max, min
For i = 0 To 9
Randomize
a(i) = Int(Rnd() * 99 + 1)
If a(i) Mod 2 = 0 Then '判断偶数,是,将存入偶数集
p = p + 1
ReDim Preserve m(p)
m(p) = a(i)
Else '不是,将存入奇数集
q = q + 1
ReDim Preserve n(q)
n(q) = a(i)
End If
Next i
max = a(0) '最大值最小值
min = a(0)
For i = 1 To 9
If a(i) > max Then max = a(i)
If a(i) < min Then min = a(i)
Next i
Print "10个数中的最大值为" & max
Print "10个数中的最小值为" & min
For p = LBound(m()) To UBound(m())
sum = sum + m(p)
Next p
avg2 = sum / UBound(m())
Print "10个数中偶数之平均数为" & avg2
sum = 0
For q = LBound(n()) To UBound(n())
sum = sum + n(q)
Next q
avg1 = sum / UBound(n())
Print "10个数中奇数之平均数为" & avg1
End Sub
Dim a(9)
Dim m(), n() 'm()为偶数集, n()为奇数集
Dim i, p, q
Dim sum
Dim avg1, avg2 'avg1为奇数平均数, avg2为偶数平均数
Dim max, min
For i = 0 To 9
Randomize
a(i) = Int(Rnd() * 99 + 1)
If a(i) Mod 2 = 0 Then '判断偶数,是,将存入偶数集
p = p + 1
ReDim Preserve m(p)
m(p) = a(i)
Else '不是,将存入奇数集
q = q + 1
ReDim Preserve n(q)
n(q) = a(i)
End If
Next i
max = a(0) '最大值最小值
min = a(0)
For i = 1 To 9
If a(i) > max Then max = a(i)
If a(i) < min Then min = a(i)
Next i
Print "10个数中的最大值为" & max
Print "10个数中的最小值为" & min
For p = LBound(m()) To UBound(m())
sum = sum + m(p)
Next p
avg2 = sum / UBound(m())
Print "10个数中偶数之平均数为" & avg2
sum = 0
For q = LBound(n()) To UBound(n())
sum = sum + n(q)
Next q
avg1 = sum / UBound(n())
Print "10个数中奇数之平均数为" & avg1
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询