用vb编一个n阶幻方

来源:百度知道 编辑:UC知道 时间:2024/05/31 07:24:47
其中n为输入的,不确定的。先在此,谢谢了!

你太懒了,帮你一下吧。
Private Sub Form_Click()
Dim n As Integer, num As Integer, row, col As Integer, i As Integer, c As Integer
Dim M() As Integer
n = InputBox("请输入一个奇数:")
If n Mod 2 = 0 Then
MsgBox "请输入一个奇数!"
Exit Sub
End If
ReDim M(n - 1, n - 1)
num = n ^ 2
row = 0
col = (n - 1) \ 2
i = 1
Do While i <= num
If row = -1 Then row = n - 1
If col = n Then col = 0
M(row, col) = i
If i Mod n = 0 Then
row = row + 1
Else
row = row - 1
col = col + 1
End If
i = i + 1
Loop
Cls
Me.FontSize = 12
Print " 输出幻方矩阵 n="; n
Print
For i = 0 To n - 1
For c = 0 To n - 1
Print Tab(c * 6); M(i, c);
Next c
Print
Next i
End Sub

Private Sub Form_Load()
Dim n As Integer, j As Integer
j = 1
n = InputBox(&qu