Next i ' 数组转置
For i = 1 To N ' For循环 i从1变化到N For j = 1 To i ' 交换 Temp = a(i, j) a(i, j) = a(j, i) a(j, i) = Temp Next j Next i
' 输出转置后的数组 Print \转置后的数组:\
For i = 1 To N ' For循环 i从1变化到N For j = 1 To N Print a(i, j); Next j Print Next i End Sub
7、参考代码如下:
Private Sub Form_Click()
Dim a(10) As Integer, i As Integer Dim sum As Integer, ave As Integer sum = 0 For i = 1 To 10
a(i) = Int(Rnd * 101) sum = sum + a(i) Next i
第 41 页 共 32 页
ave = sum / 10 Print sum, ave End Sub
8、参考代码如下:
Private Sub Form_Load() Dim i, j, k, Flower As Integer Show For i = 1 To 9 For j = 0 To 9 For k = 0 To 9
Flower = i * 100 + j * 10 + k
If Flower = i ^ 3 + j ^ 3 + k ^ 3 Then Print Flower ; \是水仙花数\ Next k Next j Next i End Sub
9、程序参考代码如下:
Private Sub Command1_Click() Dim m, i, y As Integer m = Val(Text1.Text) If m <= 2 Then
Label2.Caption = \输入数据有错\ Else
k = Sqr(m) + 1 For i = 2 To k y = m Mod i If y = 0 Then Exit For
第 42 页 共 32 页
Next i If y = 0 Then
Label2.Caption = m & \不是素数\ Else
Label2.Caption = m & \是素数\ End If End If End Sub
10、程序参考代码如下:
Option Base 1
Private Sub Form_Load()
Const N = 5 ' 常数 N 确定数组的大小 Dim a(N, N) As Integer
Dim i, j, row, line, min As Integer Show
For i = 1 To N ' For循环 i从1变化到N For j = 1 To N ' For循环 j从1变化到N a(i, j) = Int(100 * Rnd) + 1 ' 产生N*N个1~100间的随机数 Next j Next i ' 输出二维数组 Print \数组内容是:\ For i = 1 To N For j = 1 To N Print a(i, j); Next j
Print ' 输完一行元素后输出一个换行
第 43 页 共 32 页
Next i ' 设定初始值
min = a(1, 1): row = 1: line = 1 ' 寻找最小数 For i = 1 To N For j = 1 To N
If a(i, j) < min Then min = a(i, j) row = i line = j End If Next j Next i ' 输出结果
Print \最小值是:\
Print \在第\行第\列\End Sub
11、参考代码如下:
Private Sub Command1_Click()
Dim a, b, c, delt, realpart, imagpart As Single
a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text): delt = b * b - 4 * a * c If a = 0 Then
Label2.Caption = \是一元一次方程:\ If b<>0 then
Text4.Text = \ Else
第 44 页 共 32 页
If c=0 then
Text4.Text = \有无穷根\ Else
Text4.Text = \无根\ End if End if Else
' 下面嵌套块If语句 If delt = 0 Then
Label2.Caption = \有两个相等的实根:\ Text4.Text = \ Text5.Text = \ ElseIf delt > 0 Then
Label2.Caption = \有两个不等的实根:\ Text4.Text = \ Text5.Text = \ Else
Label2.Caption = \有两个共轭复根:\ realpart = -b / (2 * a) imagpart = Sqr(-delt) / (2 * a)
Text4.Text = \ Text5.Text = \ End If ' 此行End If部分与内层的块If语句对应 End If ' 此行End If部分与外层的块If语句对应 End Sub
12、参考代码如下:
Private Sub Command1_Click()
第 45 页 共 32 页