【问题标题】:visual basic input into an array视觉基本输入到数组中
【发布时间】:2016-03-22 20:19:21
【问题描述】:

我在尝试使用 Visual Basic 将整数输入到数组时遇到了一些麻烦。我对使用visual basic(和一般编程)非常陌生,我浏览了谷歌以及这个网站,试图找到答案,但是我没有找到任何运气,我想看看是否有人可以帮我一把。

基本上我到目前为止所拥有的

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

我想要实现的是询问用户他们想输入多少整数到数组中,然后允许用户输入他们选择的整数数量并将这些整数存储在数组。

如果有人可以给我一段代码示例,说明我将如何执行此操作或任何帮助,我将不胜感激。

【问题讨论】:

  • 在您的循环中,您没有向 array() 插入任何内容。你在这里要一个窗体吗?
  • 你需要读取他们想要添加多少个整数的数字。
  • 我没有使用任何形式或类似的东西,我认为这个词只是“黑匣子”。我遇到的问题是我不知道如何将输入插入到数组中,我已经尝试了一段时间,现在试图弄清楚。另外,“读取他们想要添加多少个整数的数字”是什么意思?

标签: arrays vb.net integer command-line-interface user-input


【解决方案1】:

您可以使用List 而不是Array

这是一个简短的示例(没有错误处理)

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

【讨论】:

  • 我不想你总是检查输入表单用户。
【解决方案2】:

我也会使用 ElektroStudios 提到的 List。不过,既然你用了数组,那我就这么写吧。

 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

这是一个工作示例:dotnetfiddle

【讨论】:

  • 感谢您的回复,你们非常有帮助!只是想自学如何编程,所以有时我需要一些关于逻辑结构方面的帮助。
  • 好吧..祝你好运!尝试解决问题并一一解决,您会到达那里,是的不要忘记投票并标记有帮助的答案
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2018-06-15
  • 2012-09-12
  • 2014-08-21
相关资源
最近更新 更多