onTime End Sub 实训 Private Sub Command1_Click Debug.Print quot姓名:quot Text1.Text Debug.Print quot出生年月:quot Text2.Text Debug.Print quot籍贯:quot Text3.Text If Option1.Value Then Debug.Print quot性别:quot quot男quot If Option2.Value Then Debug.Print quot性别:quot quot女quot Debug.Print quot民族:quot Combo1.Text If Check1.Value 1 Then Debug.Print quot健康quot Else Debug.Print quot不健康quot Debug.Print quot职称:quot Combo3.Text Debug.Print quot工资:quot Text4.Text Debug.Print quot外语熟练程度quot HScroll1.Value Debug.Print quot简历:quot Text5.Text End Sub 第5章 课前体验 Private Sub Command6_Click Const n 10 ‘定义常量n的值为10 Max 0: K 0 ‘最高分及所在位置赋初值 For i 1 To n bi InputBoxquot请输入第quotamp i ampquot个同学的成绩quot求最高分 ‘输入成绩 If bi gt Max Then Max bi K I ‘将第i个成绩与最高分Max相比,如果比最高分高,则保存起来 8 End If Next i Printquot最高分是第quotamp K ampquot个同学,其成绩是:quotamp Max End Sub 【例5-1】 Option Base 1 Private Sub Command1_Click Dim a3 As Integer a1 1: a2 3: a3 5 Print a1 Print a2 Print a3 End Sub 【例5-2】 For i1 To 10 bi InputBoxquot请输入第quot amp i amp quot个数quot Next i 【例5-3】 For i1 To 2 For j1 To 2 bij ij Next j Next i 【例5-4】 Dim S3,2 As Integer 程序如下: For i 0 To 3 Print Tab5 ??输出位置定位 For j 0 To 2 Si j i 2 j ‘给各元素赋值 Print Si j Next j Print ‘换行 Next i 【例5-5】 Private Sub Form_Click Dim s5 As Integer 定义数组S Const n 5 For i 1 To n si ValInputBoxquot请输入第quot amp LTrimStri amp quot个数quot 数据排序 ‘输入n个数,转换成数值后保存在数组中。
Next i For i 1 To n-1 ‘进行n-1趟比较 Max I ‘对第i遍比较时,初始假定第i个元素最小。
图5-4 使用二维数组打印出的图形 9 For j i 1 To n ‘在数组i~n个元素中选最小元素 If sj lt sMax Then Max j Next j t si si sMax sMax t ‘i~n个元素中选出的最小元素与第i个元素交换 Next i For i 1 To 5 Print si Next i End Sub 【例5-6】 Dim b As Integer Private Sub Form_Click ReDim b2 For i 0 To 2 bi i Next i ReDim Preserve b3 b3 7 For i 0 To 3 Print bi Next i End Sub 【例5-7】 Dim a8 8 As Integer 定义一个二维数组 Private Sub Form_Click ‘下面的二层循环语句给数组赋值 For i 1 To 8 For j 1 To i If i 1 Or j 1 Then ai j 1 数组中每一行第一个,最后一个数均为1 Else ai j ai - 1 j - 1 ai - 1 j 数组中其余数据等于它上一行的相邻两列之和 End If Next j Next i 下面的二层循环语句将数组中的值打印出来 For i 1 To 8 Print Tab20 - 2 i 定位打印位置 For j 1 To i If ai j lt 10 Then 将数组中的值转换成长度为3的字符串,可使打印数据整齐 s quot quot Strai j quot quot 10 ElseIf ai j lt 100 Then s quot quot Strai j End If Print s Next j Print 换行 Next i End Sub 【例5-8】 Private Sub Option1_ClickIndex As Integer Select Case Index Case 0 Label1.ForeColor RGB255 0 0 Case 1 Label1.ForeColor RGB0 255 0 Case 2 Label1.ForeColor RGB255 255 0 End Select End Sub 实训 Option Base 1 ‘定义数组下标从1开始 Dim arr ‘定义动态数组arr Private Sub Form_Click n ValInputBoxquot请输入矩阵的行数quot ‘输入矩阵行数 m ValInputBoxquot请输入矩阵的列数quot ‘输入矩阵列数 ReDim arrn m ‘重定义数组arr为n行m列的二维数组 ‘下列程序段为数组输入数据 For i 1 To n For j 1 To m arri j ValInputBoxquot请输入矩阵第quot amp i amp quot行第quot amp j amp quot列的数据quot Next j Next i ‘下列程序段在数组中找出最大值及所在位置 row 1: col 1 For i