我在VB中制作了一个二维数组,我需要帮助试图弄清楚如何在二维数组中选择一个位置,然后输入一个值。我知道如何遍历数组元素并为其添加值,但如果我只选择一个位置并输入一个值,这将是一种更快捷的方法。
由于
Console.WriteLine(“想要输入值的位置:”) userInput = Console.ReadLine
Console.WriteLine(" Would you like to enter another 1 ?? (Y or N)")
If (reply = "Y") Or (reply = "y") Then
reply = Console.ReadLine()
Console.WriteLine("What position would like to enter a value:")
userInput = Console.ReadLine
End If
For row = 0 To cRow
For column = 0 To cColumn
If grid(row, column) = 0 Then
Console.Write(Chr(32))
Else
Console.Write(Chr(42))
End If
Next
Console.WriteLine()
Next
答案 0 :(得分:0)
您需要三条信息才能为数组添加值,两个坐标(行,列)和值。假设你有这些信息,你就可以了。
Dim grid(2, 2) As String
Dim row As Integer = 1
Dim column As Integer = 0
Dim value As String = "something"
grid(row, column) = value
答案 1 :(得分:0)
如果您想获得该职位,您显然必须要求用户提供该职位。由于这是用户输入,您必须检查用户输入的内容是否有用(规则1:永远不要信任用户!)
Sub Main()
Dim matrix(8, 8) As String
Do
' get user input
Console.Write("Enter coordinates in the form: 'x,y'")
Dim input As String = Console.ReadLine()
' empty string=quit
If input.Length = 0 Then Exit Do
' split on ',' character
Dim spl = input.Split(","c)
' there must be exactly two values
If spl.Count <> 2 Then Continue Do
' check if user entered two NUMBERS
Dim x, y As Integer
If Not Integer.TryParse(spl(0), x) Or Not Integer.TryParse(spl(1), y) Then Continue Do
' Validate x
If x < 0 OrElse x > UBound(matrix, 1) Then
Console.WriteLine("x must be 0..{0}", UBound(matrix, 1))
Continue Do
End If
' Validate y
If y < 0 OrElse y > UBound(matrix, 2) Then
Console.WriteLine("y must be 0..{0}", UBound(matrix, 2))
Continue Do
End If
' set value
matrix(x, y) = "x"
Console.WriteLine("Value saved")
Loop
' output matrix
For y = 0 To UBound(matrix, 2)
For x = 0 To UBound(matrix, 1)
If String.IsNullOrEmpty(matrix(x, y)) Then
Console.Write(".")
Else
Console.Write(matrix(x, y))
End If
Next
Console.WriteLine()
Next