VB编程:利用判素数函数求100-200之间所有素数的平均值
1个回答
展开全部
Private Sub Command1_Click()
Dim i As Integer, avg As Single, n As Integer
For i = 100 To 200
If pd(i) Then
n = n + 1
avg = avg + i
End If
Next
avg = avg / n
MsgBox avg
End Sub
Private Function pd(ByVal shu As Integer) As Boolean
Dim i As Integer
For i = 2 To Sqr(shu)
If shu Mod i = 0 Then Exit For
Next
If i < Sqr(shu) Then
pd = False
Else
pd = True
End If
End Function
Dim i As Integer, avg As Single, n As Integer
For i = 100 To 200
If pd(i) Then
n = n + 1
avg = avg + i
End If
Next
avg = avg / n
MsgBox avg
End Sub
Private Function pd(ByVal shu As Integer) As Boolean
Dim i As Integer
For i = 2 To Sqr(shu)
If shu Mod i = 0 Then Exit For
Next
If i < Sqr(shu) Then
pd = False
Else
pd = True
End If
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询