程序代码:
Option Explicit
Private arrMyArray(4, 4, 4) As Integer '定义5个( 5 行 * 5 列)的数组
Private Sub Init_Swap_Show(Optional Init, Optional Arg1, Optional Arg2)
Dim i As Integer, j As Integer, k As Integer, temp As Integer, a1 As Integer, a2 As Integer, flag As Integer
If Not IsMissing(Arg1) And Not IsMissing(Arg2) Then
If Arg1 < Arg2 Then '把较小值的参数,赋值给a1,方便转换
a1 = Arg1: a2 = Arg2
Else
a1 = Arg2: a2 = Arg1
End If
End If
For i = 0 To 4
If flag = 0 Then
If Not IsMissing(Arg1) And Not IsMissing(Arg2) Then
If Arg1 >= 0 And Arg1 < 5 And Arg2 >= 0 And Arg2 < 5 Then
If i = a1 Then
flag = 1
End If
Else
Exit Sub '如果最后面两个参数不是 0 ~ 4之间的数值就退出
End If
End If
End If
For j = 0 To 4
For k = 0 To 4
If IsMissing(Init) Then arrMyArray(k, j, i) = k + j * 5 + i * 25
If flag = 1 Then
temp = arrMyArray(k, j, a2)
arrMyArray(k, j, a2) = arrMyArray(k, j, a1)
arrMyArray(k, j, a1) = temp
End If
Debug.Print arrMyArray(k, j, i),
Next k
Debug.Print
Next j
Debug.Print
If flag = 1 Then flag = 0
Next i
End Sub
Private Sub Form_Click()
Call Init_Swap_Show '初始化数据
Debug.Print "====================================分 割 线===================================="
Call Init_Swap_Show(1, Int(Rnd * 5), Int(Rnd * 5)) '随机2个方块数据互换
End Sub