以下代码已经过测试,排序采用了冒泡排序的方式。
先在窗体上添加两个文本框控件和一个按钮控件,然后将下列代码复制到你的代码窗即可实现你要的功能。
将添加的两个文本框的ScrollBars都设置成3-Both;MultiLine设置为True
Private Sub Command1_Click()
Dim i, k, n As Integer
Dim temp As Integer
Dim JiShu() As Integer
Dim Oshu() As Integer
k = 0
n = 0
For i = 1 To 100
Randomize
temp = Int(Rnd() * 100 + 100)
If temp Mod 2 = 0 Then
If k = 0 Then
ReDim Preserve Oshu(k + 1)
Oshu(k) = temp
k = k + 1
Else
If Not checkRepeat(temp, Oshu) Then
i = i - 1
Else
ReDim Preserve Oshu(k + 1)
Oshu(k) = temp
k = k + 1
End If
End If
Else
If n = 0 Then
ReDim Preserve JiShu(n + 1)
JiShu(n) = temp
n = n + 1
Else
If Not checkRepeat(temp, JiShu) Then
i = i - 1
Else
ReDim Preserve JiShu(n + 1)
JiShu(n) = temp
n = n + 1
End If
End If
End If
Next i
Call sortData(1, Oshu)
For i = 1 To k - 1
Text1.Text = Text1.Text & " " & CStr(Oshu(i))
Next i
Call sortData(2, JiShu)
For i = 1 To n - 1
Text2.Text = Text2.Text & " " & CStr(JiShu(i))
Next i
Print k, n
End Sub
Private Function checkRepeat(ss As Integer, arrdata() As Integer) As Boolean '该函数用于检查是否有重复
Dim j As Integer
For j = 0 To UBound(arrdata) - 1
If ss = arrdata(j) Then
checkRepeat = False
Exit Function
End If
Next j
checkRepeat = True
End Function
Private Function sortData(ByVal aa As Integer, ByRef arrdata() As Integer) '此函数用于排序,当参数aa为1时,采用降序,其他值时为升序
Dim i, k As Integer
Dim temp1 As Integer
If aa = 1 Then
For i = 0 To UBound(arrdata) - 1
For k = 0 To UBound(arrdata) - 1 - i
If arrdata(k + 1) < arrdata(k) Then
temp1 = arrdata(k + 1)
arrdata(k + 1) = arrdata(k)
arrdata(k) = temp1
End If
Next k
Next i
Else
For i = 0 To UBound(arrdata) - 1
For k = 0 To UBound(arrdata) - 1 - i
If arrdata(k + 1) > arrdata(k) Then
temp1 = arrdata(k + 1)
arrdata(k + 1) = arrdata(k)
arrdata(k) = temp1
End If
Next k
Next i
End If
End Function