作者fumizuki (小狮子)
看板Visual_Basic
标题Re: [VB6 ]求救
时间Fri Jun 10 09:43:43 2005
※ 引述《jyvonne78 (燃烧的云燕)》之铭言:
: 老师出了一个题目就是在1-100中随机选出5个数字,然後从新排序再利用一个程
: 式找出最大值和最小值,最後再算出平均值
: 下面我写的有没有错阿 请各位大大指教
: 乱数的我已经写出来了 可是排列到平均值就不会写了
: 虽然去看过的最大值的用法 可是他说无法执行 是不是我的范围定错了呢?
是谁说无法执行? 没有语法错误呀@@"
整个程式实在看不懂在做什麽,整个逻辑完全错了吧@@"
重写一个给你...
Private Sub Command1_Click()
'随机选数
Dim i As Integer, r(4) As Integer
Randomize
'乱数初始化
AutoRedraw = True
'自动重绘
For i = 0 To 4
r(i) = CInt(Rnd * 99 + 1)
'Rnd * (100 - 1) + 1 四舍五入
Print r(i);
If i < 4 Then Print ", "; Else Print
'输出,逗点分隔,最後一个数换行
Next
Sort r
Print "最大值:" & Max(r)
Print "最小值:" & Min(r)
Print "平均值:" & Round(Avg(r), 2)
'小数点後二位
End Sub
Private Sub Sort(a)
'由小至大排序
Dim i As Integer, j As Integer
For i = 0 To UBound(a)
For j = i + 1 To UBound(a)
If a(i) > a(j) Then Swap a(i), a(j)
Next
Next
End Sub
Private Function Max(a) As Integer
'阵列中寻找最大值
Dim i As Integer
Max = a(0)
For i = 1 To UBound(a)
If a(i) > Max Then Max = a(i)
Next
End Function
Private Function Min(a) As Integer
'阵列中寻找最小值
Dim i As Integer
Min = a(0)
For i = 1 To UBound(a)
If a(i) < Min Then Min = a(i)
Next
End Function
Private Function Avg(a) As Double
'求阵列中的平均值
Dim i As Integer, c As Integer, Sum As Integer
c = UBound(a) + 1
For i = 0 To c - 1
Sum = Sum + a(i)
Next
Avg = Sum / c
End Function
Private Sub Swap(a, b)
'两数调换
Dim c
c = a: a = b: b = c
End Sub
--
VB 程式设计 仓木麻衣 PTT 星爷板 行列输入法
======================================================
Visual_Basic MaiKuraki Stephen Array
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.58.156.43