编写函数,函数的功能是:计算n门课程的平均值,计算结果作为函数值返回。
'要求在主调过程中将5门课的成绩存入数组中,并在主调过程中输出5门课程的平均值。
'(若5门课程的成绩是:92,76,69,58,88,则函数的值为76.6。)
Private Sub Form_Click()
Dim a(1 To 5) As Integer, ave As Single
b = Array(0, 92, 76, 69, 58, 88)
For i = 1 To 5
a(i) = b(i)
Next i
ave = 【1】
Print ave
End Sub
Function pj(x%(), n%) As Single
pj = 0
For i = 1 To 【2】
pj = pj + 【3】
Next i
pj = 【4】
End Function
'要求在主调过程中将5门课的成绩存入数组中,并在主调过程中输出5门课程的平均值。
'(若5门课程的成绩是:92,76,69,58,88,则函数的值为76.6。)
Private Sub Form_Click()
Dim a(1 To 5) As Integer, ave As Single
b = Array(0, 92, 76, 69, 58, 88)
For i = 1 To 5
a(i) = b(i)
Next i
ave = 【1】
Print ave
End Sub
Function pj(x%(), n%) As Single
pj = 0
For i = 1 To 【2】
pj = pj + 【3】
Next i
pj = 【4】
End Function
举一反三
- '编写函数过程,函数的功能是:计算从1开始到n的 '自然数中偶数的平方的和,计算结果作为函数值返回 '要求在主调过程中输入n的值,并输出最终结果。 '计算从1开始到n的'自然数中偶数的平方的和 Function sn(n As Integer) As Integer s = 0 For i = 2 To 【1】 Step 2 s = s +【2】 Next i 【3】 End Function Private Sub Form_Click() Dim n As Integer, s As Integer n = InputBox("输入n") s = 【4】 '调用函数sn求1到n的平方和 Print s End Sub
- 下列程序的运行结果为( )。Private Sub Command1_Click() Dim a(5) As String Dim i As Integer For i = 0 To 5 a(i) = i + 1 Print a(i); Next iEnd Sub A: 123456 B: 6 C: 1 2 3 4 5 6 D: 0
- 运行下面的程序,第二行显示结果是___________。 Private Sub Form_Click() Dim A As Integer Dim i As Integer A = 5 For i = 1 To 9 Call sub1(i, A) Print i, A Next i End Sub Private Sub sub1(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 + 2 Next I y = y + N End Sub
- 下列程序的运行结果为( )。 Private Sub Command1_Click() Dim a(5) As String Dim i As Integer For i = 0 To 5 a(i) = i + 1 Print a(i); Next i End Sub
- 以下程序的运行结果是 Sub s1(b() As Integer) For i = 1 To 4 b(i) = 2 * i Next i End Sub Private Sub Command1_Click() Dim a(1 To 4) As Integer For i = 1 To 4 a(i) = i + 5 Next i s1 a() For i = 1 To 4 Print a(i) Next i End Sub