关于Visual Basic的一个问题

来源:百度知道 编辑:UC知道 时间:2024/06/15 22:05:14
以下两道题是我的课程作业,已经有答案了,但我不太懂,可否帮忙解释一下。感激不尽啊。

53、单击一次命令按钮之后,下列程序代码的执行结果为_B_____。
Private Sub Command1_Click ( )
S=P (1) +P (2) +P (3) +P (4)
Print S;
End Sub
Public Function P (N As Integer)
Static Sum
For i=1 To n
Sum=Sum+I
Next i
P=Sum
End Function
(A) 20 (B) 35 (C) 115 (D) 135

54、单击命令按钮,下列程序代码的执行结果为____B_______。
Private Sub Command1_Click ()
Dim firststr As String
firststr=“abcdef”
Print PickMid (firststr)
End Sub
Private Function PickMid (xstr As String) As String
Dim tempstr As string , strlen As Integer
tempstr=“”
strlen=Len (xstr)
i=1
Do While i<=Len(xstr)-3

53题:
注意函数P中第一句“Static Sum”,Sum被声明为一个静态变量,函数P会记住该变量的值,而不是像一般变量那样每次都初始化,所以有:
P(1)=0+1=1----------Sum=1
P(2)=1+1+2=4--------Sum=4
P(3)=4+1+2+3=10-----Sum=10
P(4)=10+1+2+3+4=20--Sum=20
所以S=1+4+10+20=35

54题:解释一下PickMid函数应该就可以了
Private Function PickMid(xstr As String) As String
'xstr = "abcdef"
Dim tempstr As String, strlen As Integer
tempstr = ""
strlen = Len(xstr)
'strlen=6
i = 1
Do While i <= Len(xstr) - 3
'Len(xstr) - 3 = 3
tempstr = tempstr + Mid(xstr, i, 1) + Mid(xstr, strlen - i + 1, 1)
i = i + 1
Loop
'这个Do循环的作用是从xstr字符串中取左边第一个、右边第一个字符,然后再取左边第二个、右边第二个字符,
'然后再取左边第三个,右边第三个字符,至此Do循环结束,所以tempstr="afbecd"
PickMid = tempstr
End Function

57题:
Sub作为一个过程,它的语法是这样的:
[Private | Public | Friend] [Static] Sub name [(arglist)]
[statemen