数组a中存放m个随即产生的不大于n的正整数,编写过程删除数组中相同的元素,并显示删除前和删除后的数组中的所有内容以及被删除的数据个数。
如题,我编的程序是:
Dim m%, n%, i%, Num%, j%, Same%, d%
Print Chr(13) + Chr(13); " 随机生成的原数组为:"
Dim a()
m = Val(InputBox("请输入数组元素个数m:(输入-1结束)"))
If m = -1 Then Exit Sub
n = Val(InputBox("请输入正整数n的值(输入-1结束):"))
If n = -1 Then Exit Sub
For i = 1 To m
ReDim Preserve a(i)
a(i) = Int(Rnd * n)
Print a(i);: d = d + 1
If d Mod 10 = 0 Then Print a(i)
Next i
For i = 1 To m
For j = i + 1 To m
If a(i) = a(j) Then
Same = a(j)
a(i) = "" '删除a(i)
Num = Num + 1
a(j) = ""
Num = Num + 1
End If
Next j
Next i
Print Chr(13) + Chr(13); "其中相同的元素为:"; Same
Print Chr(13) + Chr(13); "删除的相同元素个数为:"; Num
当运行时输入,100和100是就不出错
而输入1000和1000时就说:Same = a(j)“类型不匹配”
这是为什么啊?
运行出错