【问题标题】:Visual Basic ArraysVisual Basic 数组
【发布时间】:2014-04-07 17:13:41
【问题描述】:

您好,我很难在 Visual Basic 中使用数组。这是一个简单的控制台应用程序(在继续使用 gui 之前,我试图掌握语法的窍门),并且该程序所做的只是使用两种类型的数组 jagged 和常规类型。这个控制台应用程序是一个时间表生成器,即输入 5 列和 5 行来制作一个 5X5 的时间表。该程序尚未完成,我知道到目前为止,该代码不会正确生成时间表,但我需要帮助的是如何在 VB 中填充数组。我的问题出在这个子中

子:

Sub arrayPopulate(ByVal regularArray(,) As Integer, ByVal columns As Integer, ByVal rows As Integer)
        Dim i As Integer
        Dim j As Integer
        Dim mult As Integer

        For i = 0 To rows
            For j = 0 To columns
                mult = (i + 1) * (j + 1)
                regularArray(j, i) = mult
            Next
        Next

    End Sub

特别是regularArray(j, i) = mult 这一行,我认为这很简单,将数组元素设置为=mult 的任何值,for 循环将覆盖二维数组。我做错了什么,我该如何解决或做得更好?

完整代码:

 Module Module1
    Sub Main()
        'Declarations
        Dim awns As Char
        Dim switchOption As Integer
        Dim columns As Integer
        Dim rows As Integer
        Dim regularArray(columns, rows) As Integer 

        'Starting Prompts
        Console.WriteLine("Hello this program will create a times table with")
        Console.WriteLine("user inputs in terms of rows and columns.")
        Console.WriteLine("Pick between these two options.")
        Console.WriteLine("Option 1: Times table with a regular array.")
        Console.WriteLine("Option 2: Times table with a jagged array.")

        Do
            Console.Write("Which option do you wnat? ")
            switchOption = Console.ReadLine

            Console.WriteLine("How many columns do you wnat? ")
            columns = Console.ReadLine
            columns = columns - 1
            Console.WriteLine("How many rows do you wnat? ")
            rows = Console.ReadLine
            rows = rows - 1
            Select Case switchOption
                Case 1
                    arrayPopulate(regularArray, columns, rows)
                    Dim i As Integer
                    Dim j As Integer

                    For j = 0 To rows
                        For i = 0 To columns
                            Console.WriteLine("{0}: ", regularArray(i, j))
                        Next
                    Next

                Case 2
                    Console.WriteLine("Test")

            End Select

            Console.WriteLine("Do you want to run again y/n?")
            awns = Console.ReadLine()
        Loop Until awns = "n"
    End Sub

    Sub arrayPopulate(ByVal regularArray(,) As Integer, ByVal columns As Integer, ByVal rows As Integer)
        Dim i As Integer
        Dim j As Integer
        Dim mult As Integer

        For i = 0 To rows
            For j = 0 To columns
                mult = (i + 1) * (j + 1)
                regularArray(j, i) = mult
            Next
        Next

    End Sub
End Module

【问题讨论】:

    标签: arrays vb.net basic


    【解决方案1】:

    在您声明Dim regularArray(columns, rows) As Integer 的地方,它使用了当时的columnsrows 的值;在这种情况下,它们都为零,因此regularArray 是一个具有一个元素的二维数组 - 即regularArray(0, 0)。使用ReDim 更改其尺寸的大小。例如,将其放在 Select Case switchOption 之前:

    ReDim regularArray(columns, rows)
    

    详情请见http://msdn.microsoft.com/en-us/library/w8k3cys2.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2021-10-03
      • 2023-03-06
      • 1970-01-01
      • 2019-02-03
      • 2013-07-13
      相关资源
      最近更新 更多