[英]Visual Basic Arrays
您好,我在Visual Basic中很难处理数组。 这是一个简单的控制台应用程序(在继续使用gui之前,我试图了解语法的基本知识),并且此程序所做的所有工作都是使用两种锯齿状数组和常规类型。 该控制台应用程序是一个时间表生成器,即输入5列和5行以制作5X5时间表。 该程序尚未完成,到目前为止,我知道使用此代码不会正确生成时间表,但是我需要帮助的是如何在VB中填充数组。 我的问题在哪里
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
具体地,线regularArray(j, i) = mult
我会认为这是简单的阵列元素被设定=
到什么都mult
是和for循环将包括2D阵列。 我在做什么错,如何解决或做得更好?
完整代码:
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
在您将Dim regularArray(columns, rows) As Integer
,它利用了当时的columns
和rows
的值; 在这种情况下,它们都为零,因此regularArray
是具有一个元素的二维数组,即regularArray(0, 0)
。 使用ReDim
更改其尺寸的大小。 例如,将其放在Select Case switchOption
之前:
ReDim regularArray(columns, rows)
有关详细信息,请参见http://msdn.microsoft.com/en-us/library/w8k3cys2.aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.