VB 为什么Print出来的第二行是0,第三行是4 a(1)不应该 = f(1)= 2 a(4)= f(4)= 5 么?

2025-03-20 22:11:28
推荐回答(1个)
回答(1):

Private Sub Command1_Click()
Dim a(6) As Integer
Dim x As Integer, i As Integer
Print f(2)
For i = 1 To 5
Debug.Print i, '增加这两句,在立即窗口中看打印内容就知道了
a(i) = f(i)
Debug.Print a(i), i '增加这两句,在立即窗口中看打印内容就知道了
Next i
Print a(1)
Print a(4)
End Sub
Function f(x As Integer)
x = x + 1
f = x
End Function
'立即窗口打印结果:
' 1 2 2 i=1时,经过f函数,变为2,未给a(1)赋值,所以为0
' 3 4 4
' 5 6 6