vb.net 在 Visual Basic 中将输入存储到数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31128835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:14:57  来源:igfitidea点击:

Storing input into Arrays in Visual Basic

vb.net

提问by Juju

I'm just got into Visual Basic and I'm trying to "recode" my programs from java into VB. But my main problem is how to do that, i don't the syntax too much. I have read some but I find it hard(I'm a slow learner :P).

我刚刚开始使用 Visual Basic,并且正在尝试将我的程序从 Java“重新编码”到 VB 中。但我的主要问题是如何做到这一点,我不太了解语法。我读过一些,但我觉得很难(我学习速度很慢:P)。

Edit: Here is the code I am trying:

编辑:这是我正在尝试的代码:

Module Module1
Dim arrays(5) As String

Sub Main()
    Console.WriteLine("Enter your Names:")
    For i As Integer = 0 To arrays.Length
        arrays(i) = Console.ReadLine
    Next i

    For Each arr As String In arrays
        Console.WriteLine(arr)
    Next
    Console.ReadLine()
End Sub

End Module

终端模块

At some point, whenever I run it and try to input, it goes beyond the number of index. And doesn't write the inputs :P

在某些时候,每当我运行它并尝试输入时,它都会超出索引的数量。并且不写输入:P

回答by OneFineDay

Since it is a zero based array, you need to get the length - 1. Your array is set to 5, so it has 6 elements and arrays. Length = 6 where your loop needs to be 0 to 5.

由于它是基于零的数组,因此您需要获取长度 - 1。您的数组设置为 5,因此它有 6 个元素和数组。长度 = 6,其中您的循环需要为 0 到 5。

Module Module1
Dim arrays(5) As String

 Sub Main()
  Console.WriteLine("Enter your Names:")
  For i As Integer = 0 To arrays.Length - 1
    arrays(i) = Console.ReadLine
  Next i

  For Each arr As String In arrays
    Console.WriteLine(arr)
  Next
  Console.ReadLine()
 End Sub
End Module