
大神 VB用一維陣列 代碼怎么撰寫?跪求啦!謝謝
uj5u.com熱心網友回復:
可以這樣寫:
dim arr(1 to 25) as integer
a(1) = 1
' ...
a(25) = 9
uj5u.com熱心網友回復:
愛的魔力轉圈圈
Option Explicit
Dim aryOut(0 To 24) As Byte ' 一維陣列
Private Sub Command1_Click()
Dim intDirect As Integer '方向 0 右 1下 2左 3上
Dim x As Integer, y As Integer '當前填寫位置在矩陣中的坐標
Dim i As Integer
For i = 1 To 25 ' 25個數 挨個填入一維陣列。當前位置從矩陣坐標 (0,0)開始 方向從0右開始
aryOut(GetAryPos(x, y)) = i '數值填入
getNext x, y, intDirect
Next
'產生輸出字串
Dim strout As String
For i = LBound(aryOut) To UBound(aryOut)
strout = strout & Format(aryOut(i), "00") & " "
If (i + 1) Mod 5 = 0 Then strout = strout & vbCrLf
Next
'列印輸出
Debug.Print strout
End Sub
'根據矩陣坐標,獲取其在陣列中的下標
Private Function GetAryPos(ByVal x As Integer, ByVal y As Integer) As Integer
GetAryPos = 5 * y + x
End Function
'邊緣檢測 根據矩陣坐標,方向,獲取下一個坐標,并看看該坐標上的資料是否已經有值(碰到邊緣了)
Private Sub getNext(ByRef x As Integer, ByRef y As Integer, ByRef direct As Integer, Optional turntimes As Integer = 0)
Dim tmpx As Integer, tmpy As Integer
Dim Turnflag As Boolean '是否轉向標志
If turntimes > 4 Then Exit Sub
'沿著輸入的方向前進一步,獲取坐標
tmpx = x: tmpy = y
Select Case direct
Case 0
tmpx = x + 1
Case 1
tmpy = y + 1
Case 2
tmpx = x - 1
Case 3
tmpy = y - 1
End Select
If tmpx > 4 Or tmpx < 0 Or tmpy > 4 Or tmpy < 0 Then
'如果超出矩陣范圍了,檢測到邊緣,需要順時針轉向
Turnflag = True
Else
'沒有超出矩陣范圍,檢測到矩陣已經填值了,需要順時針轉向,否則不需要
Turnflag = IIf(aryOut(GetAryPos(tmpx, tmpy)) > 0, True, False)
End If
If Turnflag Then
'如果需要轉向,那么方向改變
direct = (direct + 1) Mod 4
'然后嘗試取轉向后的下一步
getNext x, y, direct, turntimes + 1
Else
'不需要轉向,回傳已經走了一步的坐標
x = tmpx: y = tmpy
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/23419.html
標籤:VB基礎類
