用vb随机产生10个10~90间的整数,再计算其中的素数个数。为什么我的代码做出来不对?求正解

2024-12-15 02:00:32
推荐回答(2个)
回答1:

程序逻辑太乱,看不太懂,但有几个问题
1,变量声明中,c as integer里c 后面的空格去哪了?
2,a =int(sqn(j(i)))中的sqn在这里起什么作用?sqn是返回参数的正负值的一个标志,按你的程序,这里a永远是1,那这个表达式是没有意义的。
3,for b=2 to a这个循环永远只有一次,一点意义也没有
dim i as integer, a as integer,b as integer,c as string, d as integer,j(10) as integer
c=""
d=0
for i= 1 to 10
randomize
j(i)= int(rnd*81+10)
if j(i) mod b=0 then
exit for
else
d=d+1
end if
c=c & “ ” & str(j(i))
next i
text1.text=c
text2.text=d

回答2:

大概按你的思路改了改。

好像代码应该如下:
Private Sub Command1_Click()
Dim i As Integer, a As Integer, b As Integer, c As String, d As Integer, j(10) As Integer
c = ""
For i = 1 To 10
Randomize
j(i) = Int(Rnd * 81 + 10)
a = Int(Sqr(j(i)))
For b = 2 To a
If j(i) Mod b = 0 Then
Exit For
End If
Next b
If b > a Then
d = d + 1
End If
c = c & Str(j(i))
Next i
Text1.Text = c
Text2.Text = d
End Sub