Public Function suiji(ByVal min As Integer, ByVal max As Integer) As Integer
Dim res As Integer
Randomize()
If max < min Then
res = 0
Else
res = Int(Rnd() * (max - min + 1) + min)
End If
Return res
End Function
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim a(20), temp As Integer
Dim str As String = ""
For i = 1 To 20
a(i) = suiji(10, 100)
str = str & a(i) & " "
Next
str = str & vbCrLf
For i = 1 To 20
For j = 1 To 20 - i
If a(j) > a(j + 1) Then
temp = a(j + 1)
a(j + 1) = a(j)
a(j) = temp
End If
Next j
Next i
For i = 1 To 20
str = str & a(i) & " "
Next
TextBox6.Text = str
End Sub
运行结果:
61 18 89 33 17 91 25 43 13 12 81 57 13 53 75 100 67 98 73 80
12 13 13 17 18 25 33 43 53 57 61 67 73 75 80 81 89 91 98 100
追问大神可以用子过程写么?谢谢 老师说不能用Function
追答Dim str As String = ""
Sub suiji()
Dim a(20), temp As Integer
For i = 1 To 20
a(i) = Int(Rnd() * (100 - 10 + 1) + 10)
str = str & a(i) & " "
Next
str = str & vbCrLf
For i = 1 To 20
For j = 1 To 20 - i
If a(j) > a(j + 1) Then
temp = a(j + 1)
a(j + 1) = a(j)
a(j) = temp
End If
Next j
Next i
For i = 1 To 20
str = str & a(i) & " "
Next
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Call suiji()
TextBox6.Text = str
End Sub
上面这个使用了子过程