求大神帮看VB代码哪边出错了——随机生成10个20到90之间的整数,将其中的素数输出.Private Sub Form_Click()Dim a(1 To 10),i As IntegerDim m,n,j,t As IntegerFor i = 1 To 10Randomizea(i) = Int(Rnd * 71 + 20)Print a(i);Next iPr
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/22 11:54:04
求大神帮看VB代码哪边出错了——随机生成10个20到90之间的整数,将其中的素数输出.Private Sub Form_Click()Dim a(1 To 10),i As IntegerDim m,n,j,t As IntegerFor i = 1 To 10Randomizea(i) = Int(Rnd * 71 + 20)Print a(i);Next iPr
求大神帮看VB代码哪边出错了——随机生成10个20到90之间的整数,将其中的素数输出.
Private Sub Form_Click()
Dim a(1 To 10),i As Integer
Dim m,n,j,t As Integer
For i = 1 To 10
Randomize
a(i) = Int(Rnd * 71 + 20)
Print a(i);
Next i
Print
t = a(i)
m = Sqr(t)
For j = 2 To m
If t Mod j = 0 Then
Exit For
End If
Next
Print "素数为:"; a(t)
End Sub
求大神帮看VB代码哪边出错了——随机生成10个20到90之间的整数,将其中的素数输出.Private Sub Form_Click()Dim a(1 To 10),i As IntegerDim m,n,j,t As IntegerFor i = 1 To 10Randomizea(i) = Int(Rnd * 71 + 20)Print a(i);Next iPr
1) t= a(i) 这句下标越界了, 因为这时i =11
2) 判断素数的语句也有点逻辑上的错误.
3) 随机数有可能重复.
我改了下,请参考:
Private Sub Form_Click()
Dim a(1 To 10), i As Integer
Dim m, n, j, t As Integer
Dim Is_prime_number As Boolean
For i = 1 To 10
Randomize
a(i) = Int(Rnd * 71 + 20)
Print a(i);
Next i
Print
For i = 1 To 10
t = a(i)
m = Int(Sqr(t))
Is_prime_number = True
For j = 2 To m
If t Mod j = 0 Then
Is_prime_number = False
Exit For
End If
Next j
If Is_prime_number = True Then Print "素数为:"; a(i)
Next i
End Sub