[英]Programming syntax - searching an array for a string matching input - visual basic
[英]visual basic input into an array
我在嘗試使用Visual Basic將整數輸入數組時遇到一些麻煩。 我是使用Visual Basic(和一般編程)的新手,我在Google以及該網站上都進行了嘗試,試圖找到答案,但是我找不到運氣,我想看看是否有人可以幫我一下
基本上我到目前為止
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
我要實現的目的是詢問用戶他們想要輸入多少個整數,然后允許用戶輸入他們選擇的整數數量並將這些整數存儲在數組中。
如果有人可以給我一個示例代碼,說明我將如何執行此操作或提供任何幫助,我將非常感激。
您可以使用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
我還將使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.