0

我有一些代码,其中我将“地图”存储在字符串中,然后将其放入数组中。该数组已将字符串分隔为每个索引位置的字符。我现在想将这些字符放入一个二维数组中,如下所示是我的尝试:

Sub DisplayMap()
    Dim MapTog As String
    MapTog = ("--------------------------------------------------------------------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-------------------                                        ---------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("--------------------------------------------------------------------------------")
    Dim MapSep() As Char = MapTog.ToCharArray
    For Count = 0 To 24
        For Count2 = 0 To 79
            Map(Count)(Count2) = MapSep(Count2)
            Console.Write(MapSep(Count2))
        Next Count2
    Next Count
    Console.SetCursorPosition(0, 0)

End Sub

我认为我的 For 循环有问题,但我无法弄清楚。非常感谢任何帮助,谢谢!:)

4

1 回答 1

0

这是一种方法:

Module Module1

    Public Map As New List(Of List(Of Char))

    Public Sub Main()
        CreateMap()
        DisplayMap()

        Console.ReadLine()
    End Sub

    Public Sub DisplayMap()
        Console.Clear()
        ' this demonstrates how you can access each char separately:
        For y As Integer = 0 To Map.Count - 1
            For x As Integer = 0 To Map(y).Count - 1
                Console.SetCursorPosition(x, y)
                Console.Write(Map(y)(x))
            Next
        Next

        ' ... or you could do ...

        Console.Clear()
        ' this demonstrates how you can convert each row to a string
        For y As Integer = 0 To Map.Count - 1
            Console.SetCursorPosition(0, y)
            Console.Write(New String(Map(y).ToArray))
        Next
    End Sub

    Public Sub CreateMap()
        Dim MapTog As String = _
             "--------------------------------------------------------------------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-------------------                                        ---------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "--------------------------------------------------------------------------------"

        Map.Clear()
        For Each line As String In MapTog.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
            Map.Add(New List(Of Char)(line.ToCharArray))
        Next
    End Sub

End Module
于 2013-06-07T15:38:57.203 回答