VB.net 菱形數字

VB.net 菱形數字

寫這題目有點麻煩,所以分兩段來寫,寫的不是很好,應該有更好的方法吧

這邊有VB6的版本,不太好改

圖片 5


Module Module1
    Sub Main()
        Dim i, n As Integer
        Dim ans, pans, tmp, xy, xy2 As String
        n = 5
        For i = 1 To n
            Select Case i
                Case 1
                    For k = 16 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 2
                    For k = 12 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 3
                    For k = 8 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 4
                    For k = 4 To 1 Step -1
                        Console.Write(" ")
                    Next k
            End Select
            ans = ans & i
            pans = ans
            Do While Len(pans) < n
                pans = "  " & pans
            Loop
            If i >= 2 Then tmp = i - 1 & tmp 
            xy = pans & tmp
            xy = Trim(xy)
            For j = 1 To Len(Trim(xy))
                Console.Write(xy.Substring(j - 1, 1) + "   ")
            Next j
            Console.WriteLine()
        Next i
        For j = 1 To n - 1
            Select Case j
                Case 1
                    For k = 4 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 2
                    For k = 8 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 3
                    For k = 12 To 1 Step -1
                        Console.Write(" ")
                    Next k
                Case 4
                    For k = 16 To 1 Step -1
                        Console.Write(" ")
                    Next k
            End Select
            pans = " " & Mid(pans, 1, n - 1)
            tmp = Mid(tmp, 2, n - 2)
            xy2 = pans & tmp
            xy2 = Trim(xy2)
            For j2 = 1 To Len(Trim(xy2))
                Console.Write(xy2.Substring(j2 - 1, 1) + "   ")
            Next j2
            Console.WriteLine()
        Next j
        Console.Read()
    End Sub
End Module

 


如有錯誤 歡迎指正