下面代码是本人在网上搜到的,使用时出现数组的第一个值未进行比较,故改之,并做了简单的其他方面使用的提示;方便后者使用,我想这也是第一发帖人的初衷吧。
'下面这段代码有问题,x(0)无法比较
Public Function maxmin(x() As Double)
Dim n1 As Integer,n2 As Integer
Dim i As Integer
n1 = UBound(x)
n2 = LBound(x)
MaxNumber = x(n1)
MinNumber = x(n2)
For i = n2 + 1 To n1
If x(i) > MaxNumber Then
MaxNumber = x(i)
End If
Next i
For i = n2 + 1 To n1
If x(i) < MinNumber Then
MinNumber = x(i)
End If
Next i
Print "最大值为" & MaxNumber
Print "最小值为" & MinNumber
End Function
'上面代码应改为如下即可使用了,如果比较绝对值则使用abs函数'MaxNumber = abs(x(n1))'MinNumber = abs(x(n2))'If abs(x(i)) > MaxNumber ThenPublic Function maxmin(x() As Double) Dim n1 As Integer,n2 As Integer Dim i As Integer n1 = UBound(x) n2 = LBound(x) MaxNumber = x(n1) MinNumber = x(n2) For i = n2 To n1 If x(i) >= MaxNumber Then 'If abs(x(i)) > MaxNumber Then MaxNumber = x(i) End If Next i For i = n2 To n1 If x(i) <= MinNumber Then MinNumber = x(i) End If Next i Print "最大值为" & MaxNumber Print "最小值为" & MinNumberEnd Function
原文链接:https://www.f2er.com/vb/263269.html