VB利用随机函数Rnd生成10个10-99的整数,并计算其中的素数个数。(求大神看看哪里错了?)
PrivateSubCommand1_Click()RandomizeDimiAsInteger,jAsInteger,nAsInteger,nlAsInteger,st...
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As Integer
For i = 1 To 10
n = Int(Rnd * 99 + 10)
st = st & n & " "
For j = 2 To n - 1
If n Mod j = 0 Then Exit For
Next j
If j = n Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub 展开
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As Integer
For i = 1 To 10
n = Int(Rnd * 99 + 10)
st = st & n & " "
For j = 2 To n - 1
If n Mod j = 0 Then Exit For
Next j
If j = n Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub 展开
5个回答
展开全部
我给你优化了一下,由于最大99,所以只需要判断到9就可以了,一旦j=10,则为素数
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As Integer
For i = 1 To 10
n = Int(Rnd * 90 + 10)
st = st & n & " "
For j = 2 To 9
If n Mod j = 0 Then Exit For
Next j
If j = 10 Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As Integer
For i = 1 To 10
n = Int(Rnd * 90 + 10)
st = st & n & " "
For j = 2 To 9
If n Mod j = 0 Then Exit For
Next j
If j = 10 Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
n = Int(Rnd * 90 + 10)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已经改好了:
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As String
For i = 1 To 10
n = Int(Rnd * 99 + 10)
st = st & n & " "
For j = 2 To n - 1
If n Mod j = 0 Then Exit For
Next j
If j = n Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer, n As Integer, nl As Integer, st As String
For i = 1 To 10
n = Int(Rnd * 99 + 10)
st = st & n & " "
For j = 2 To n - 1
If n Mod j = 0 Then Exit For
Next j
If j = n Then
nl = nl + 1
End If
Next i
Text1.Text = st
Text2.Text = nl
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询