VB.net 棒球九宮格

用VB.net 寫棒球九宮格

這個範例是別人要我幫忙寫的作業,題目是 棒球九宮格,每次數字排列不同,輸入數字可以連線(顯示有幾條連線)

我是用C# 主控台應用程式 寫的,有應用到 陣列 及for 迴圈 及 IF 判斷,也不算是太難的題目

 

圖片 1

    Sub Main()
        Dim i, num, temp As Integer
        Dim n As Integer = 9
        Dim A(9) As Integer
        Dim y As String

        For i = 1 To n
            A(i) = i
        Next
        Randomize()
        Dim stringA As String
        stringA = "" 
        For i = n To 1 Step -1          '不重複亂數
            num = Fix(i * Rnd()) + 1
            temp = A(i)
            A(i) = A(num)
            A(num) = temp
        Next
        For i = 1 To 9         '取前9個
            stringA &= Space(8 - 2 * Len(Str(A(i)))) & Str(A(i)) & "  "    '文字對齊
            If i Mod 3 = 0 Then stringA &= vbNewLine
        Next
        Console.WriteLine(stringA)
        Console.WriteLine("請輸入9次數字")
        Dim Ostring As String
        Ostring = ""
        For ii = 1 To 9 '投9球
            y = Console.ReadLine
            If IsNumeric(y) AndAlso y > 0 AndAlso y < 10 Then
                For iii = 1 To 9
                    If A(iii) > 0 And A(iii) < 10 Then
                        If y = A(iii) Then
                            Ostring += "A" & iii
                        End If
                    End If
                Next
            End If

        Next
        Dim zz As Integer
        zz = 0

        If InStr(Ostring, "A1") And InStr(Ostring, "A2") And InStr(Ostring, "A3") Then
            zz += 1
        End If
        If InStr(Ostring, "A4") And InStr(Ostring, "A5") And InStr(Ostring, "A6") Then
            zz += 1
        End If
        If InStr(Ostring, "A7") And InStr(Ostring, "A8") And InStr(Ostring, "A9") Then
            zz += 1
        End If

        If InStr(Ostring, "A1") And InStr(Ostring, "A4") And InStr(Ostring, "A7") Then
            zz += 1
        End If
        If InStr(Ostring, "A2") And InStr(Ostring, "A5") And InStr(Ostring, "A8") Then
            zz += 1
        End If
        If InStr(Ostring, "A3") And InStr(Ostring, "A6") And InStr(Ostring, "A9") Then
            zz += 1
        End If

        If InStr(Ostring, "A1") And InStr(Ostring, "A5") And InStr(Ostring, "A9") Then
            zz += 1
        End If
        If InStr(Ostring, "A3") And InStr(Ostring, "A5") And InStr(Ostring, "A7") Then
            zz += 1
        End If
        Console.WriteLine("總共有" & zz & "條連線")

        Console.ReadLine()
    End Sub

範例下載


如有錯誤 歡迎指正