# 請幫我註解(vb陣列)

Dim x(6, 5)

Private Sub Command1_Click()

Open "c:\baseball.txt" For Input As #1

Do While Not EOF(1)

r = r + 1

Input #1, x(r, 1), x(r, 2), x(r, 3), x(r, 4)

x(1, 5) = "打擊績效"

x(6, 1) = "平均"

If r <> 1 Then x(r, 5) = x(r, 2) + x(r, 3) + x(r, 4)

For i = 2 To 5

x(6, i) = (x(2, i) + x(3, i) + x(4, i) + x(5, i)) / 4

Next i

Print x(r, 1), x(r, 2), x(r, 3), x(r, 4), x(r, 5)

Loop

Print x(6, 1), x(6, 2), x(6, 3), x(6, 4), x(6, 5)

Close #1

End Sub

### 2 個解答

• 匿名使用者
2 0 年前
最佳解答

Dim x(6, 5) '宣告一個二維陣列

Private Sub Command1_Click() 'Command1 按鈕被按下時引發的事件

Open "c:\baseball.txt" For Input As #1 '以 input方式開啟 c:\baseball.txt 檔案

Do While Not EOF(1) '若檔案還沒到結尾則做迴圈內statement

r = r + 1 'r變數遞增1

Input #1, x(r, 1), x(r, 2), x(r, 3), x(r, 4) '讀入4筆資料並放入 x 陣列的 (r,1) ~ (r,4) 中

x(1, 5) = "打擊績效" 'x的 (1,5) 放入 "打擊績效" 字串

x(6, 1) = "平均" 'x的 (6,1) 放入 "平均" 字串

If r <> 1 Then x(r, 5) = x(r, 2) + x(r, 3) + x(r, 4) '如果 r 不等於1的話 x(r,5) 的值等於x(r, 2) + x(r, 3) + x(r, 4)

For i = 2 To 5 '迴從從2到5

x(6, i) = (x(2, i) + x(3, i) + x(4, i) + x(5, i)) / 4 'x(6, i)的值放入x(2, i),x(3, i),x(4, i),x(4, i),這4個值相加後除以4的值,也就是平均值

Next i

Print x(r, 1), x(r, 2), x(r, 3), x(r, 4), x(r, 5) '印出x陣列的 (r,1) ~ (r,5) 五個元素中的值

Loop

Print x(6, 1), x(6, 2), x(6, 3), x(6, 4), x(6, 5) '印出x陣列的 (6,1) ~ (6,5) 五個元素中的值

Close #1 '關檔

End Sub '事件結束

• Lv 6
2 0 年前

Dim x(6, 5)

Private Sub Command1_Click()

Open "c:\baseball.txt" For Input As #1

Do While Not EOF(1)

r = r + 1

Input #1, x(r, 1), x(r, 2), x(r, 3), x(r, 4)

x(1, 5) = "打擊績效"

x(6, 1) = "平均"

If r <> 1 Then x(r, 5) = x(r, 2) + x(r, 3) + x(r, 4)

For i = 2 To 5

x(6, i) = (x(2, i) + x(3, i) + x(4, i) + x(5, i)) / 4

Next i

Print x(r, 1), x(r, 2), x(r, 3), x(r, 4), x(r, 5)

Loop

Print x(6, 1), x(6, 2), x(6, 3), x(6, 4), x(6, 5)

Close #1

End Sub

記得程式要「適當縮排」才容易閱讀，

依宣告及程式內容，若你的資料過多會有錯誤訊息出現，

資料內容應該只有四筆，

每筆有四個data，但第一個為空白，所以只有三個data

最後一欄打擊績效為三個data的總和

最後再計算總平均