vb编程实例
PrivateSubCommand1_Click()DimpAsIntegerp=m(1)+m(2)+m(3)PrintpEndSubPrivateFunctionm(n...
Private Sub Command1_Click()
Dim p As Integer
p = m(1) + m(2) + m(3)
Print p
End Sub
Private Function m(n As Integer) As Integer
Static s As Integer
For k = 1 To n
s = s + 1
Next
m = s
End Function
为什么结果是10,求解~~万分感谢!! 展开
Dim p As Integer
p = m(1) + m(2) + m(3)
Print p
End Sub
Private Function m(n As Integer) As Integer
Static s As Integer
For k = 1 To n
s = s + 1
Next
m = s
End Function
为什么结果是10,求解~~万分感谢!! 展开
2个回答
展开全部
Static s As Integer s 是静态变量,过程结束后,数据依然存在,且值保持原来不变
一开始s=0
m(1),走入Function,时,n=1 ,循环一次(加上一个1),s=1,m(1)=1
m(2),走入Function,时,s=1(值保持原来1不变),n=2 ,循环2次(加上2个1),s=3,m(2)=3
m(3),走入Function,时,s=3(值保持原来3不变),,n=3 ,循环3次(加上3个1),s=6,m(3)=6
m(1)+m(2)+m(3)=1+3+6=10
一开始s=0
m(1),走入Function,时,n=1 ,循环一次(加上一个1),s=1,m(1)=1
m(2),走入Function,时,s=1(值保持原来1不变),n=2 ,循环2次(加上2个1),s=3,m(2)=3
m(3),走入Function,时,s=3(值保持原来3不变),,n=3 ,循环3次(加上3个1),s=6,m(3)=6
m(1)+m(2)+m(3)=1+3+6=10
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询