请高手帮我解读一下这段VB的程序,为什么按了3次命令按钮之后会是得16

来源:百度知道 编辑:UC知道 时间:2024/05/14 15:03:37
Private Sub Command1_Click()
Dim total As Integer
total = s(1) + s(2)
Print total

End Sub
Private Function s(m As Integer) As Integer
Static x As Integer
For i = 1 To m
x = x + 1
Next i
s = x

End Function

因为x为静态变量,保留上一次执行该过程时的值.
第一次s(1)=1,s(2)=3 '3=1+2
第二次s(1)=4,s(2)=6 '4=3+1,6=4+2
第三次s(1)=7,s(2)=9 '7=6+1,9=7+2
而每次输出的都是s(1)+s(2)的值

该为:
total = s(1) + s(2)
Print s(1); s(2); total
自己研究吧。