试图显示矩形阵列(更新)

时间:2011-08-02 19:06:06

标签: vb.net arrays

我正在尝试为练习做一个tic-tac-toe控制台应用程序。我创建了一个case语句,供用户选择他们想要填写的方格。

Public Class TwoDi
    Shared Sub twodimension()
        Console.WriteLine("Do you want to go first?")
        first = Console.ReadLine()

        If first = "yes" Then
            Console.WriteLine("Please enter a X or O.")
            x2 = Console.ReadLine()

            Console.WriteLine("Pick a spot.")
            pickone(x2)
        End If
    End Sub

    Shared Sub pickone(ByVal x As String)
        Dim choice As String
        Dim tic(2, 2) As String
        Dim x2 As String
        Dim bound0 As Integer = tic.GetUpperBound(0)
        Dim bound1 As Integer = tic.GetUpperBound(1)
        Dim i As Integer
        Dim x1 As Integer
        Dim populate As String = "n"

        For i = 0 To bound0
          For x1 = 0 To bound1
            Console.Write("{0}", show)
          Next
         Console.WriteLine()
        Next

        x2 = x
        choice = Console.ReadLine()

        Select Case choice
            Case "middle"    //putting in one case statement for example.
                choice = "middle"
                tic(1, 1) = x2        
        End Select

        Console.Write("" & vbLf & "Pick a spot.")
        pickone(x2)
    End Sub
End Class

现在我尝试了几个循环来更新显示以显示3x3 2-d阵列。特别是这一个:

For i = 0 To bound0
    For x1 = 0 To bound1
        Console.Write("{0}", tic(bound0, bound1))
    Next
    Console.WriteLine()
Next

我使用“n”预填充3x3网格,但是当我运行循环时,它只显示一个x或o,甚至没有正确间隔。

这也是一个有点单独的问题,当我循环再次运行一段代码时,最好再次调用Sub / Function吗?

1 个答案:

答案 0 :(得分:1)

解决“所有填充X或O的问题,具体取决于我选择X或O”部分:

你真的打算输入Console.Write("{0}", tic(1, 1))而不是这个吗?

For i = 0 To bound0
     For x1 = 0 To bound1
       Console.Write("{0}", tic(i, x1))
     Next
   Console.WriteLine()
Next