'窗体只需要一个command控件
Private Sub Command1_Click()
Dim i, k, a(1 To 80), j(), o(), jj, oo, t
Print "随机生成80个10~99之间的整数:"
Randomize
For i = 1 To 80
a(i) = Int(Rnd * 90) + 10
If a(i) Mod 2 = 1 Then
jj = jj + 1
ReDim Preserve j(jj)
j(jj) = a(i)
Else
oo = oo + 1
ReDim Preserve o(oo)
o(oo) = a(i)
End If
Print a(i);
If i Mod 10 = 0 Then Print
Next
Print vbCrLf & "奇数按由大到小的顺序排列:"
For i = 1 To jj
For k = 1 To i
If j(i) > j(k) Then t = j(i): j(i) = j(k): j(k) = t
Next
Next
For i = 1 To jj
Print j(i);
If i Mod 10 = 0 Then Print
Next
Print vbCrLf & "偶数按由小到大的顺序排列:"
For i = 1 To oo
For k = 1 To i
If o(i) < o(k) Then t = o(i): o(i) = o(k): o(k) = t
Next
Next
For i = 1 To oo
Print o(i);
If i Mod 10 = 0 Then Print
Next
t = 0
k = 0
Print vbCrLf & "其中的素数为:"
For i = 1 To jj
If prime(j(i)) Then
t = t + j(i)
k = k + 1
Print j(i);
If k Mod 10 = 0 Then Print
End If
Next
Print vbCrLf & "素数的个数为:" & k
Print "素数的和为:" & t
End Sub
Private Function prime(ByVal n As Long) As Boolean '返回值=True是素数,False不是素数
Dim k As Long
If n < 2 Then
prime = False
Else
prime = True
For k = 2 To n - 1
If (n Mod k) = 0 Then prime = False: Exit For
Next k
End If
End Function
温馨提示:内容为网友见解,仅供参考