VB利用随机函数Rnd生成10个10-99的整数,并计算其中的素数个数。(求大神看看哪里错了?)

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

1、首先,我们给主函数先定一个函数。

2、然后我们编写For循环语句。

3、然后我们编写输出语句及范围。

4、然后我们开始运行测试。

5、这句警告没关系,如果不想看到可以添加调用数学函数

6、这时我们便可以看到效果了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-12
我给你优化了一下,由于最大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
第2个回答  2014-12-11
n = Int(Rnd * 90 + 10)本回答被提问者采纳
第3个回答  2014-12-11
已经改好了:
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
第4个回答  2018-01-05
st As String