数组形式,循环比较被
Private Sub Form_Load()
Dim i As Integer, x() As Double, a As Integer, Max As Double, Min As Double
ReDim x(10)
For i = 1 To 10
Randomize
x(i) = Rnd * 100 + 1
Print x(i)
Next i
Max = 0
Min = x(1)
For i = 1 To 10
If x(i) > Max Then
Max = x(i)
End If
If x(i) < Min Then
Min = x(i)
End If
Next i
Print vbCrLf
Print Max; vbCrLf
Print Min
End Sub
给你一个100个数组中产生的1到10000的随机数的最大值和最小值:
Dim X(1 To 100) As Long, I As Integer, J As Integer, S As Long
Private Sub Command1_Click()
For I = 1 To 100
Randomize
X(I) = Int(10000 * Rnd + 1)
Next I
For I = 1 To 99
For J = 1 To 100 - I
If X(J) > X(J + 1) Then
S = X(J)
X(J) = X(J + 1)
X(J + 1) = S
End If
Next J
Next I
Label1.Caption = "最小值=" & Str(X(1))
Label2.Caption = "最大值=" & Str(X(100))
End Sub