求一个VB编程具体代码,编写一个求个数中最值的过程,然后分别求几种情况的最值。具体见详细描述。

2024-11-07 15:29:51
推荐回答(1个)
回答1:

尝试了一下,方法如下:
1.新建一个模块,输入以下代码:
Option Explicit
Public Type Mxx
nMax As Integer
nMin As Integer
End Type

Public Sub getMxx(ByRef aSrc() As Integer, ByRef nRet As Mxx)
Dim i As Integer, m As Integer, n As Integer
m = LBound(aSrc)
n = UBound(aSrc)
nRet.nMax = aSrc(1)
nRet.nMin = aSrc(1)
For i = m + 1 To n
If aSrc(i) > nRet.nMax Then
nRet.nMax = aSrc(i)
End If
If aSrc(i) < nRet.nMin Then
nRet.nMin = aSrc(i)
End If
Next
End Sub

2.新建一个窗体,有3个按钮,输入以下代码:
Option Explicit
' 3个数
Private Sub Command1_Click()
Dim a(1 To 3) As Integer
Dim b As Mxx
a(1) = 1
a(2) = 3
a(3) = 2
getMxx a(), b
Print b.nMin, b.nMax
End Sub

' 5个数
Private Sub Command2_Click()
Dim a(1 To 5) As Integer
Dim b As Mxx
a(1) = 1
a(2) = -3
a(3) = 2
a(4) = 4
a(5) = -2
getMxx a(), b
Print b.nMin, b.nMax
End Sub

' 7个数
Private Sub Command3_Click()
Dim a(1 To 7) As Integer
Dim b As Mxx
a(1) = 123
a(2) = -321
a(3) = 0
a(4) = 414
a(5) = -252
a(6) = -1024
a(7) = 9876
getMxx a(), b
Print b.nMin, b.nMax
End Sub

3.运行一下,看看效果。