展开全部
求最大值就是,取第一个数给临时变量,然后用临时变量去跟数组的数比较,如果数组的值大于临时变量的值,则用数组的值替换临时变量的值,数组检索完成最后得到的就是数组里面最大的值。取最小值也是一样的道理。
例子,取最大最小值:
Private Function Max(Data() As Double) As Double '求实数最大值函数
Dim i As Long
Dim x1 As Long, x2 As Long
x1 = LBound(Data)
x2 = UBound(Data)
Max = Data(x1)
For i = x1 To x2
If Data(i) >= Max Then Max = Data(i) '如果数组当前值比Max值大,替换Max中的值
Next
End Function
Private Function Min(Data() As Double) As Double '求实数最小值函数
Dim i As Long
Dim x1 As Long, x2 As Long
x1 = LBound(Data)
x2 = UBound(Data)
Min = Data(x1)
For i = x1 To x2
If Data(i) <= Min Then Min = Data(i) '如果数组当前值比Min值大,替换Min中的值
Next
End Function
Private Sub Command1_Click() '点击按钮求随机数组最大值
Dim a As Integer, b As Integer
Dim Maxs As Double, Mins As Double
Dim temps(100) As Double
Randomize '定义生成的随机数为不重复模式
For i = 0 To 100
If Rnd() > 0.5 Then temps(i) = 100 * Rnd() Else temps(i) = -100 * Rnd() '生成101个100以内的随即实数
Next
Maxs = Max(temps) '调用Max函数求实数最大值
Mins = Min(temps) '调用Min函数求实数最小值
Me.Cls
Me.Print Maxs '显示实数到窗体
Me.Print Mins
End Sub
例子,取最大最小值:
Private Function Max(Data() As Double) As Double '求实数最大值函数
Dim i As Long
Dim x1 As Long, x2 As Long
x1 = LBound(Data)
x2 = UBound(Data)
Max = Data(x1)
For i = x1 To x2
If Data(i) >= Max Then Max = Data(i) '如果数组当前值比Max值大,替换Max中的值
Next
End Function
Private Function Min(Data() As Double) As Double '求实数最小值函数
Dim i As Long
Dim x1 As Long, x2 As Long
x1 = LBound(Data)
x2 = UBound(Data)
Min = Data(x1)
For i = x1 To x2
If Data(i) <= Min Then Min = Data(i) '如果数组当前值比Min值大,替换Min中的值
Next
End Function
Private Sub Command1_Click() '点击按钮求随机数组最大值
Dim a As Integer, b As Integer
Dim Maxs As Double, Mins As Double
Dim temps(100) As Double
Randomize '定义生成的随机数为不重复模式
For i = 0 To 100
If Rnd() > 0.5 Then temps(i) = 100 * Rnd() Else temps(i) = -100 * Rnd() '生成101个100以内的随即实数
Next
Maxs = Max(temps) '调用Max函数求实数最大值
Mins = Min(temps) '调用Min函数求实数最小值
Me.Cls
Me.Print Maxs '显示实数到窗体
Me.Print Mins
End Sub
展开全部
Sub aa()
Dim i&
i = Max(Range("A1:D18"))
mxgbox i
End Sub
Dim i&
i = Max(Range("A1:D18"))
mxgbox i
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dim a,b
dim max
max=Max(a,b)
dim max
max=Max(a,b)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一楼不对,首先要定义max()作用的
If a>b
then max=a
else
max=b
If a>b
then max=a
else
max=b
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询