编写VB程序:在窗体上输入一个n行、n列、主对角线和次对角线元素为1,其余元素均为零的矩阵。n由用户规定

2025-01-25 04:33:59
推荐回答(3个)
回答1:

Private Sub Form_Activate()

Dim n As Integer

n = 10  '用户在这里规定

Dim a(10) As String

For i = 1 To n

    a(i) = ""

   For j = 1 To n

       If i = j Or i - j = 1 Or j - i = 1 Then

            a(i) = a(i) & " " & 1

       Else

           a(i) = a(i) & " " & 0

      End If

      

Next

Next

For i = 0 To n

    Print a(i)

Next

End Sub

回答2:

Option Explicit
Option Base 1
Dim a(), n%, i%, j%
Private Sub Command1_Click()
Cls
n = Text1.Text
ReDim a(n, n)
For i = 1 To n
For j = 1 To n
If i = j Or i + j = n + 1 Then
a(i, j) = 1
Else
a(i, j) = 0
End If

Next j
Next i
For i = 1 To n
For j = 1 To n
Print a(i, j);
Next j
Print
Next i
End Sub

回答3:

Private Sub Form_Click()

Dim a(1 To 5, 1 To 5) As Integer
For i = 1 To 5
a(i, i) = 1
a(i, 6 - i) = 1
Next i

For j = 1 To 5
For k = 1 To 5
Print a(j, k);
Next k
Print
Next j

End Sub