律师挂靠在律所的费用:一个很简单的求质数的程序,看看哪错了

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 00:38:53
ReDim sushu(n) As Integer
i = 0
Do While i >= 0
For a = 3 To 100
m = True
For b = 2 To a - 1
If a Mod b = 0 Then
m = False
End If
Next b
If m = True Then

sushu(i) = a
i = i + 1
End If

Next a
Loop
为什么会一直下标越界??

i=0
sushu(i)=...
basic的下标如果不特别说明,是从1开始的,所以shushu(0)越界了。

还有你的ReDim sushu(n) As Integer 中n没有定义,则认为n=0
相当于ReDim sushu(0) As Integer ,你说越界不越界?

Dim sushu(100) As Integer
i = 0
For a = 3 To 100
m = True
For b = 2 To a - 1
If a Mod b = 0 Then
m = False
Exit For
End If
Next b
If m = True Then
i = i + 1
sushu(i) = a
End If
Next a
For n = 1 To i
Print sushu(n);
Next

你定义的数组
ReDim sushu(n) As Integer

n要是个明确的值!不然n=0,所以你定义的sunshu里只有一个元素 sushu(0)

读到sushu(1)的时候会提示下标越界

你这是用VB编的吧?其中有二处错误,但是我也不能肯定。