有关于VB数组的.
Dim i, j, z As Integer
i = 0: j = n
Print "逆置前数组的值:"
For i = 1 To 10
a(i) = Int(Rnd * 100)
Print a(i);
Next i
Print
Do
n = InputBox("请输入n的值")
Loop Until n >= 1 And n <= 10
Print
Print "输入的n的值为:"; n
Do While i < j
z = a(i)
a(i) = a(j)
a(j) = z
i = i + 1
j = j - 1
Loop
Print "逆置后数组的值:"
For i = 0 To n
Print a(i);
Next i
Print
End Sub
这是逆置数组问题,逆置后放在同一数组输出。
但是为什么总是报有错呢?
答案:这样就行了:
Private Sub Form_Click()
Randomize
Me.AutoRedraw = True
Cls
Dim i As Integer, j As Integer, z As Integer, n As Integer
Dim a(10) As Integer
Print "逆置前数组的值:"
For i = 1 To 10
a(i) = Int(Rnd * 100)
Print a(i);
Next i
Print
Do
n = InputBox("请输入n的值")
Loop Until n >= 1 And n <= 10
Print
Print "输入的n的值为:"; n
i = 1: j = n
Do While i < j
z = a(i)
a(i) = a(j)
a(j) = z
i = i + 1
j = j - 1
Loop
Print "逆置后数组的值:"
For i = 1 To n
Print a(i);
Next i
Print
End Sub
改动了三处:
一是i = 0: j = n 的位置做了调整
二是i = 0: j = n 改成了i = 1: j = n
三是For i = 0 To n 改成了For i = 1 To n
已经运行过。
上一个:跪求高手写vb代码
下一个:q求vb的视频教程