VB编程问题
1、随机生成10个三位正整数,统计其中奇数和偶然的个数,将统计结果显示在窗体上2、 将100以内的素数求出来显示在图片框Picture1中
3、任意生成10个两位正整数,使用选择法对其进行升序排列
4、随机生成10个两位正整数,将其中的奇数和偶数分别显示在list1和list2中
2、 将100以内的素数求出来显示在图片框Picture1中
3、任意生成10个两位正整数,使用选择法对其进行升序排列
4、随机生成10个两位正整数,将其中的奇数和偶数分别显示在list1和list2中
答案:'第一题答案
Private Sub Form_Click()
Dim a(1 To 10) As Integer
Randomize Timer
Print "随机产生的数据是:"
For i = 1 To 10
a(i) = Int(Rnd * 900 + 100)
Print a(i); " ";
If a(i) Mod 2 <> 0 Then
js = js + 1
Else
os = os + 1
End If
Next i
Print "奇数共有:" & js & "个"
Print "偶数共有:" & os & "个"
End Sub
'第二题答案
Private Sub Form_Click()
For i = 2 To 100
If Prime(i) Then
Picture1.Print i; " ";
b = b + 1
If b Mod 10 = 0 Then Picture1.Print
End If
Next i
End Sub
Function Prime(a) As Boolean
For i = 2 To Sqr(a)
If a Mod i = 0 Then
Prime = False
Exit Function
End If
Next i
Prime = True
End Function
'第三题答案
Private Sub Form_Click()
Dim a(1 To 10) As Integer
Randomize Timer
Print "产生的数据是:"
For i = 1 To 10
a(i) = Int(Rnd * 90 + 10)
Print a(i); " ";
Next i
For i = 1 To 9
For j = i + 1 To 10
If a(i) < a(j) Then
c = a(i)
a(i) = a(j)
a(j) = c
End If
Next j
Next i
Print "排序后的数据是:"
For i = 1 To 10
Print a(i); " ";
Next i
End Sub
'第四题答案
Private Sub Form_Click()
Dim a(1 To 10) As Integer
Randomize Timer
Print "产生的数据是:"
For i = 1 To 10
a(i) = Int(Rnd * 90 + 10)
Print a(i); " ";
If a(i) Mod 2 <> 0 Then
List1.AddItem a(i)
Else
List2.AddItem a(i)
End If
Next i
End Sub