vb 中如何初始化数组?

2024年11月15日 02:20
有5个网友回答
网友(1):

看我这个行不行,先给数组赋一些随机数,然后再去统计某个数在其中出现的次数。
“Int((upperbound - lowerbound + 1) * Rnd + lowerbound)是产生指定区间的随机数 ,这里,upperbound 是随机数范围的上限,而 lowerbound 则是随机数范围的下限。
这样数组就赋好值了。”然后比如要统计10在这个数组中出现了几次可以定义个变量N来记录它的出现次数,代码如下:

Dim a(9) As Integer '定义数组a有10个元素
Dim i As Integer
dim n as integer '记录指定数在数组中出现的次数
For i = 0 To 9 '给数组a 赋值
Randomize
a(i) = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Next i
n=0
for i=0 to 9
if a(i)=10 then n=n+1
next i
print n

网友(2):

假设定义了3个数组,分别为a(5)、b(6)、c(7)。如果要统计某个数在数组中出现的次数,那么就定义一个变量i,初值为0。然后每个数组查询一遍,有出现就累加。

Private Sub Command1_Click()

Dim A() As Integer
Dim B() As Integer
Dim C() As Integer
Dim CounterA As Integer
Dim CounterB As Integer
Dim CounterC As Integer
Dim I As Integer
Dim J As Integer
Dim K As Integer

CounterA = InputBox("请输入数组A的大小:", "请输入数组A的大小")
ReDim A(CounterA)
For J = 0 To CounterA - 1
A(J) = InputBox("请输入数组A" & J + 1 & "的值:", "请输入数组A" & J + 1 & "的值")
Next
CounterB = InputBox("请输入数组B的大小:", "请输入数组B的大小")
ReDim B(CounterB)
For J = 0 To CounterB - 1
B(J) = InputBox("请输入数组B" & J + 1 & "的值:", "请输入数组B" & J + 1 & "的值")
Next
CounterC = InputBox("请输入数组C的大小:", "请输入数组C的大小")
ReDim C(CounterC)
For J = 0 To CounterC - 1
C(J) = InputBox("请输入数组C" & J + 1 & "的值:", "请输入数组C" & J + 1 & "的值")
Next

K = InputBox("请输入需要查找的数值:", "请输入需要查找的数值")
I = 0
For J = 0 To CounterA - 1
If A(J) = K Then
I = I + 1
End If
Next
For J = 0 To CounterB - 1
If A(J) = K Then
I = I + 1
End If
Next
For J = 0 To CounterC - 1
If A(J) = K Then
I = I + 1
End If
Next

MsgBox "数值" & K & "出现次数为: " & I & " 次"

End Sub

网友(3):

该示例使用 Array 函数来返回一个包含 Variant 的数组。
Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
'返回值假设下界的设置为 1(使用 Option Base
' 语句)。
MyDay = MyWeek(2) 'MyDay 的值为 "Tue"。
MyDay = MyWeek(4) 'MyDay 的值为 "Thu"。

网友(4):

如果初始值没有规律,要用赋值语句,
如果有规律,则可以用循环赋值来初始化.

网友(5):

可以的.

dim a()

a=array("1","5","6","4")