Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Long, y As Long, z As String, m As Long, n As Long, now As String
n = InputBox("请输入数字金字塔的层数!")
For y = 1 To n
z = Space(n - y)
For x = 1 To y * 2 - 1
m = x
If m > y Then m = y * 2 - x
now = (m.ToString)
z = z + now
Next x
ListBox1.Items.Add(z)
z = ""
Next y
End Sub
End Class
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
如果要加空格分开
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Long, y As Long, z As String, m As Long, n As Long, now As String
n = InputBox("请输入数字金字塔的层数!")
For y = 1 To n
z = Space((n - y) * 2)
For x = 1 To y * 2 - 1
m = x
If m > y Then m = y * 2 - x
now = (m.ToString)
z = z + now + " "
Next x
ListBox1.Items.Add(z)
z = ""
Next y
End Sub
End Class
\\\\\\\\\
哦对了,VS2008的,05应该没问题:P
===============================
应邀做个2位数以上的对齐,整体相对整齐些
这个不可能完美,除非绘图模式,参见第九第十层即可了解,"9"和"10"不能纵轴完美对称,而用空格调节使两端对齐的话又非正常格式.
"___1_2_3_4_5_6_7_8_9_8_7_6_5_4_3_2_1___"
"1_2_3_4_5_6_7_8_9_10_9_8_7_6_5_4_3_2_1_"
=================================
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Long, y As Long, z As String, m As Long, n As Long, now As String
n = InputBox("请输入数字金字塔的层数!")
'------------------------取最长行总字符
Dim a As Long, b As Long, c As Long, lens As String
For a = 1 To n * 2 - 1
b = a
If b > n Then b = n * 2 - b
now = (b.ToString)
lens = lens + now + " "
Next a
c = Len(Trim$(lens))
'------------------------
For y = 1 To n
'z = Space((n - y) * 2)
For x = 1 To y * 2 - 1
m = x
If m > y Then m = y * 2 - x
now = (m.ToString)
z = z + now + " "
Next x
'++++++
z = Space((c - Len(Trim$(z))) / 2) + z
'++++++
ListBox1.Items.Add(z)
z = ""
Next y
End Sub
End Class
////////////////////////////
或者
////////////////////////////////////////////////////////////
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Long, y As Long, z As String, m As Long, n As Long, now As String
Dim fmrt As String
n = InputBox("请输入数字金字塔的层数!")
For x = 1 To Len(Trim$(n)) : fmrt = fmrt + "0" : Next
'------------------------取最长行总字符
Dim a As Long, b As Long, c As Long, lens As String
lens = ""
For a = 1 To n * 2 - 1
b = a
If b > n Then b = n * 2 - b
now = Format(b, fmrt)
lens = lens + now + " "
Next a
c = Len(Trim$(lens))
'------------------------
For y = 1 To n
'z = Space((n - y) * 2)
z = ""
For x = 1 To y * 2 - 1
m = x
If m > y Then m = y * 2 - x
now = Format(m, fmrt)
z = z + now + " "
Next x
'++++++
z = Space((c - Len(Trim$(z))) / 2) + z
'++++++
ListBox1.Items.Add(z)
z = ""
Next y
End Sub
End Class
Private Sub form_Click()
For i = 1 To 7
For k = 1 To i
Print k;
Next
For k = i - 1 To 1 Step -1
Print k;
Next
Print
Next
End Sub
-----------------------
你说的居中 是不是下面这种
Const s = "123456789"
Private Sub form_Click()
Dim i As Integer
For i = 1 To 9
Print Space(9 - i) & Left(s, i) & StrReverse(Left(s, i - 1))
Next
End Sub
我用VB6.0测试通过
Private Sub Form_Click()
Dim i As Long, j As Long, n As Long
n = InputBox("请输入数字金字塔的层数!")
For i = 1 To n
For j = 1 To i
Print Tab(5 * (n - i + j)); j;
Next j
For j = i - 1 To 1 Step -1
Print Tab(5 * (n + i - j)); j;
Next j
Print
Next i
End Sub
我的最简单: 2位数以上的对齐也能用
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer, j As Integer, n As Integer, str As String, temp As String
n = InputBox("请输入数字金字塔的层数:")
For i = 1 To n
str = "" : str = Space((n - i) * (Len(CType(n, String)) + 1))
For j = 1 To i * 2 - 1
temp = ""
str = str & " " & Format(IIf(j > i, i * 2 - j, j), temp.PadLeft(Len(CType(n, String)), "0"))
Next j
ListBox1.Items.Add(str)
Next i
End Sub
End Class
Private Sub Command1_Click()
x = 2 '你想要几行,就改成几
For i = 1 To x * 2 Step 2
Me.Print Space(x - i / 2 + 0.5); String(i, "*")
Next i
End Sub