早教吧 育儿知识 作业答案 考试题库 百科 知识分享

vb中,用不同定义一样的数组,在一个算法里,结果却不一样?具体如下第一组:Dimstr,I%DimAI(31)AsSingleAR=Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28

题目详情
vb中,用不同定义一样的数组,在一个算法里,结果却不一样?具体如下
第一组:
Dim str, I%
Dim AI(31) As Single
AR = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
一个算法
print结果 (是错误的)
第二组:
Dim ar(31) As Single
For I = 0 To 31
ar(I) = I + 1
print ar(i) '这个地方的一堆数字和上一组array中的一样
Next I
一个算法
print 结果(是对的)
但我需要第一组的方式来进行编程,该如何改正呢?
▼优质解答
答案和解析
注意:
Array()函数,只能给一个Variant类型的数组赋值!!!
要这样:
Dim AR() As Variant
AR = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
或者
Dim AR()
AR = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
或者
Dim AR
AR = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
或者
Dim AR As Variant
AR = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
For I = 0 To 31
Print AR(I)
Next I