2012年计算机考试二级VB考点及习题(8)
(2)static变量
Private Sub Command1_Click()
Dim n As Integer, i As Integer
n = 2
For i = 9 To 1 Step -1
Call sub2(i, n)
Print i, n
Next i
End Sub
Private Sub sub2(x As Integer, y As Integer)
Static n As Integer
Dim i As Integer
For i = 3 To 1 Step -1
n = n + x
x = x - 1
Next i
y = y + n
End Sub
(3) 递归
Private Sub Command1_Click()
Dim a As Integer
a = 2
Call sub1(a)
End Sub
Private Sub sub1(x As Integer)
x = x * 2 + 1
If x < 10 Then
Call sub1(x)
End If
x = x * 2 + 1
Print x
End Sub
(4)递归
Private Sub test(x As Integer)
Dim i As Integer
If x <> 0 Then
Call test(x - 1)
For i = 1 To x
Print x;
Next i
End If
End Sub
Private Sub Form_Click()
test 3
End Sub
(5)同名变量
Dim y As Integer
Private Sub Form_Click()
Dim x As Integer,y as integer
x=1 : y=1
Print "x1=";x, "y1=";y
Test
Print "x4=";x, "y4=";y
End Sub
Private Sub Test()
Dim x As Integer
Print "x2=";x, "y2=";y
x=2 : y=3
Print "x3=";x,"y3=";y
End Sub
特例:
1、运行程序在窗体显示的结果是:
Private Sub Form_Click()
Dim a As Integer, b As Integer
a = 3: b = 4
Call sub1(a, a)
Print a, b
Call sub1(b, b)
Print a, b
End Sub
Private Sub sub1(x As Integer, y As Integer)
x = x + 2
y = x + y
End Sub