简体   繁体   English

视觉基本输入到数组中

[英]visual basic input into an array

I'm having some trouble trying to input integers into an array using visual basic. 我在尝试使用Visual Basic将整数输入数组时遇到一些麻烦。 I'm very new to use visual basic (and programming in general) and I've had a look around google as well as, this website to try and find an answer however I am not finding any luck and I wanted to see if anyone could lend me a hand. 我是使用Visual Basic(和一般编程)的新手,我在Google以及该网站上都进行了尝试,试图找到答案,但是我找不到运气,我想看看是否有人可以帮我一下

Basically what I have so far 基本上我到目前为止

Function inputArray()
    Dim array() As Integer
    Console.WriteLine("Please input how many integers you would like to add")
    For i = 0 To array.Length - 1
        Console.WriteLine("Please enter an integer")
        Console.ReadLine()
    Next
    Console.WriteLine(array)
    Return array
End Function

What i'm trying to achieve is to ask the user how many integers they would like to input into the array and then allow the user to make an input for the amount of integers they have chosen and to store those integers in the array. 我要实现的目的是询问用户他们想要输入多少个整数,然后允许用户输入他们选择的整数数量并将这些整数存储在数组中。

If anyone could possibly give me an example piece of code as to how I would do this or any help at all I would really appreciate it. 如果有人可以给我一个示例代码,说明我将如何执行此操作或提供任何帮助,我将非常感激。

You could use a List instead of an Array . 您可以使用List而不是Array

This is a short example (without error-handlings) 这是一个简短的示例(没有错误处理)

Imports system.Threading

Module Module1

    Sub Main()
        Module1.BuildIntegerList()

        Console.ReadKey()
        Environment.Exit(exitCode:=0)
    End Sub

    Private Sub BuildIntegerList()

        Dim values As New List(Of Integer)
        Dim amount As Integer
        Dim nextValue As Integer

        Console.WriteLine("Please input how many integers you would like to add")
        amount = CInt(Console.ReadKey().KeyChar.ToString())

        Do Until values.Count = amount
            Console.Clear()
            Console.WriteLine("Please enter an integer")
            nextValue = CInt(Console.ReadKey().KeyChar.ToString())
            values.Add(nextValue)
            Thread.Sleep(250)
        Loop

        Console.Clear()
        Console.WriteLine(String.Format("Values: {0}", String.Join(", ", values)))

    End Sub

End Module

I would also use List as mentioned by ElektroStudios. 我还将使用ElektroStudios提到的List。 However, since you used arrays, here's how I would write it. 但是,由于您使用了数组,因此这是我的写法。

 Function inputArray()
     Console.WriteLine("Please input how many integers you would like to add")
     Dim count = CInt(console.ReadLine())
     Dim array(count-1) As Integer 

     For i = 0 To count - 1
            Console.WriteLine("Please enter an integer")
            array(i) = CInt(Console.readline())
     Next

      For i = 0 To array.Length-1
        Console.Write(array(i))
      Next

   return array
 End Function

Here is a working example : dotnetfiddle 这是一个工作示例: dotnetfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM