用VB编写可连续生成十个0至100之间的随机整数

编写Windows应用程序,可连续生成十个0至100之间的随机整数,每次生成随机数后,都依序将其连接到一个字符串中,同时还需将其中的偶数联接到另一个字符串中。随机数序列生成结束后,用Label控件显示十个随机数构成的字符串。用Labe2显示其中偶数构成的字符串。要在每位数后面加上一个空格或者一个“,”

第1个回答  2008-09-21
Private Sub Command1_Click()
Dim I As Long, S(1 To 10) As Long, Str1 As String, Str2 As String

For I = 1 To 10
Randomize
S(I) = Rnd() * 100
Next
Call PaiXu(S) '排序数组
For I = 1 To 10
Str1 = Str1 & S(I) & " "
If S(I) Mod 2 = 0 Then Str2 = Str2 & S(I) & " "
Next
Label1.Caption = Str1: Label2.Caption = Str2
End Sub

Private Sub PaiXu(Zu As Variant, Optional BigInBefore As Boolean)
'排序数组 Zu(),BigInBefore=True 表示大的在前(降序),默认升序
Dim I As Long, J As Long, K As Long, IsBig As Boolean
Dim Tmp() As Long, nL As Long, nU As Long, S As Long

nL = LBound(Zu): nU = UBound(Zu)

'将 Zu() 存入临时数组 Tmp()
ReDim Tmp(nL To nU)
For I = nL To nU
Tmp(I) = Zu(I)
Next

For I = nL To nU
S = Tmp(I) '从临时数组提取第 I 个数
'在已经排序好的 Zu() 中搜索,共 I-1 个
'升序时:看 S 是否比其中某数小
'降序时:看 S 是否比其中某数大
For J = nL To I - 1
IsBig = S < Zu(J)
If BigInBefore Then IsBig = Not IsBig
If IsBig Then
'后移 J 后面所有已经排序好的 Zu()
For K = I To J + 1 Step -1
Zu(K) = Zu(K - 1)
Next
Zu(J) = S: GoTo XiaI '将 S 安插到位置 J
End If
Next
'在已经排序好的 Zu() 中没有找到,将 S 安排到末尾(位置 I)
Zu(I) = S
XiaI:
Next
End Sub
第2个回答  2008-09-21
Private Sub Form_Load()
Randomize
Dim intRnd As Integer
Dim strRnd As String
Dim strOushu As String
Dim i As Integer
For i = 0 To 9
intRnd = Int(rnd * 100)
strRnd = strRnd & intRnd & ","
If intRnd Mod 2 = 0 Then strOushu = strOushu & intRnd & ","
Next
Label1.Caption = strRnd
Label2.Caption = strOushu
End Sub本回答被提问者采纳
第3个回答  2008-09-21
Dim rnd(9) As Byte

Private Sub Form1_Load()
Randomize()
End Sub
Private Sub Command1_Click()
For i = 0 To 9
rnd(i) = CInt(Rnd(1) * 100)
Label1.Caption = Label1.Caption + " " + rnd(i)
If Mod(rnd(i),2) = 0 Then
Label2.Caption = Label2.Caption + " " + rnd(i)
End If
Next
End Sub