(二)实验内容
利用随机函数产生60个100~200之间的随机整数,将其中的偶数按由小到大的顺序排列并输出,将奇数按由大到小的顺序排列并输出。
(三)实验要求
1.将随机产生的60个整数存入数组,按每行10个数输出到窗体或文本框中。
2.偶数和奇数也分别存入数组,将排好序的偶数和奇数分别输出到窗体或两个文本框中。
3.界面由学生自行设计。
希望高手们能帮助小弟作答,答案最好不是网络上的,谢谢大家啦!!!!!!
Private Sub Form_DblClick()
Dim s1(1 To 60) As Integer
Dim s2(1 To 60) As Integer
Dim i As Integer, j As Integer, k As Integer, t As Integer
Text1 = ""
For i = 1 To 60 '产生60个不重复随机数
Randomize
Do
s1(i) = 100 + Int(101 * Rnd)
If InStr(Text1.Text, Str(s1(i)) + ",") = 0 Then
If i Mod 10 = 0 Then '换行控制
Text1 = Text1 & Str(s1(i)) & "," & Chr(13) + Chr(10)
Else
Text1 = Text1 & Str(s1(i)) & ","
End If
Exit Do
End If
Loop While (1)
Next
j = 1
For i = 1 To 60 '偶数整理
If s1(i) Mod 2 = 0 Then
s2(j) = s1(i)
j = j + 1
End If
Next
k = j
For i = 1 To 60 '奇数整理
If s1(i) Mod 2 = 1 Then
s2(j) = s1(i)
j = j + 1
End If
Next
For i = 1 To k - 1 '偶数排序
For j = i + 1 To k
If s2(i) > s2(j) Then
t = s2(i): s2(i) = s2(j): s2(j) = t
End If
Next
Next
Text2 = ""
For i = 1 To k
If i Mod 10 = 0 Then '偶数换行控制
Text2 = Text2 & Str(s2(i)) & "," & Chr(13) + Chr(10)
Else
Text2 = Text2 & Str(s2(i)) & ","
End If
Next
For i = k + 1 To 59 '奇数排序
For j = i + 1 To 60
If s2(i) < s2(j) Then
t = s2(i): s2(i) = s2(j): s2(j) = t
End If
Next
Next
Text3 = ""
For i = k + 1 To 60
If i - k Mod 10 = 0 Then '奇数换行控制
Text3 = Text3 & Str(s2(i)) & "," & Chr(13) + Chr(10)
Else
Text3 = Text3 & Str(s2(i)) & ","
End If
Next
End Sub
'界面说明:三个多行文本框,执行效果如下: