VB.NET-多个用户输入整数到arrayList



我是编程的新手...我正在尝试编码一个非常基本的程序,以便用户可以输入10个整数,然后平均。我已经考虑使用ArrayList存储所有数据,但是一旦我无法再次使用"输入"。

我应该做的是采用这样的基本平均程序:

Dim A, B, C, D, E, F, G, H, I, J As Integer
A = 10
B = 6
C = 17
...
...
...
J = 15
Dim K As Double
K = A + B + C + D + E + F + G + H + I + J
K /= 10
Console.WriteLine(K)
Console.ReadKey()

...并做到这一点,以便用户可以输入所有变量。

希望这个问题很清楚...有人知道我能做什么?

我在下面包括,并带有注释的示例。希望您将从陈述中学习,并了解如何使用Do/LoopFor循环以及如何使用List(Of )存储可变量的数据。

Sub Main()
    ' Initialize variable for text input, and numeric value
    Dim input As String, x As Double
    ' Initialize empty array of numbers
    Dim array = New List(Of Double)()
    Do
        Console.Write("Enter a number or press [Enter] to Finish :")
        ' Read a number (as text)
        input = Console.ReadLine()
        ' Check if input is a number
        If (Double.TryParse(input, x)) Then
            ' If it is a number add it to list
            array.Add(x)
        ElseIf x.Length>0
            ' If not then display a message
            Console.WriteLine("** Input Not Numeric **")
        End If
        ' Continue until user presses enter
    Loop Until input.Length = 0
    Console.WriteLine("{0} Numbers Entered", array.Count)
    ' Calculate average from values
    Dim average As Double = 0
    For index As Integer = 1 To array.Count
        average += array(index - 1)
    Next
    average /= array.Count
    ' Display results and wait for enter
    Console.WriteLine("The Average Is {0}", average)
    Console.Write("Press [Enter] to End")
    Console.ReadLine()
End Sub

我会按步骤做事。首先。

    Dim a, b, c, d As Integer
    Console.Write("Type an integer: ")
    a = Int32.Parse(Console.ReadLine())
    Console.Write("Type an integer: ")
    b = Int32.Parse(Console.ReadLine())
    Console.Write("Type an integer: ")
    c = Int32.Parse(Console.ReadLine())
    Console.Write("Type an integer: ")
    d = Int32.Parse(Console.ReadLine())
    Console.WriteLine("The average is: " & ((a + b + c + d) / 4))
    Console.ReadLine()

然后将代码转换为使用列表

    Dim v As New List(Of Integer)
    Console.Write("Type an integer: ")
    v.Add(Int32.Parse(Console.ReadLine()))
    Console.Write("Type an integer: ")
    v.Add(Int32.Parse(Console.ReadLine()))
    Console.Write("Type an integer: ")
    v.Add(Int32.Parse(Console.ReadLine()))
    Console.Write("Type an integer: ")
    v.Add(Int32.Parse(Console.ReadLine()))
    Console.WriteLine("The average is: " & ((v(0) + v(1) + v(2) + v(3)) / 4))
    Console.ReadLine()

现在您正在使用列表,可以轻松使用循环删除重复代码。

    Dim v As New List(Of Integer)
    Do While v.Count < 4
        Console.Write("Type an integer: ")
        v.Add(Int32.Parse(Console.ReadLine()))
    Loop
    Console.WriteLine("The average is: " & ((v(0) + v(1) + v(2) + v(3)) / 4))
    Console.ReadLine()

之后,您添加了错误验证并使用内置的平均功能。

    Dim tempVal As Integer
    Dim v As New List(Of Integer)
    Do While v.Count < 4
        Console.Write("Type an integer: ")
        If Int32.TryParse(Console.ReadLine(), tempVal) Then
            v.Add(tempVal)
        Else
            Console.WriteLine("Not a valid integer")
        End If
    Loop
    Console.WriteLine("The average is: " & v.Average())
    Console.ReadLine()

相关内容

  • 没有找到相关文章

最新更新