VB中的动态数组

时间:2018-08-31 23:31:41

标签: vb.net

在这里苦苦挣扎的学生...我被要求找到最小,最大和中点值。但是,我无法弄清楚。阵列/数据存储距离3章距。所以,我希望有人可以帮助我解决这个难题。

-我需要从十进制数组中的单个元素获取数据。有任何想法吗?我在很大程度上不成功使用“ array.getvalue”方法和“ array.min”方法。

Dim numberOfInvoices As Integer
Dim totalOfInvoices As Decimal
Dim invoiceAverage As Decimal

Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim subtotal As Decimal = CDec(txtEnterSubtotal.Text)
    Dim discountPercent As Decimal = 0.25D
    Dim discountAmount As Decimal = Math.Round(subtotal * discountPercent, 2)
    Dim invoiceTotal As Decimal = subtotal - discountAmount
    Dim minimum, maximum, middle, i As Decimal
    Dim array(1) As Decimal
    Dim midpoint As Integer

    txtSubtotal.Text = FormatCurrency(subtotal)
    txtDiscountPercent.Text = FormatPercent(discountPercent, 1)
    txtDiscountAmount.Text = FormatCurrency(discountAmount)
    txtTotal.Text = FormatCurrency(invoiceTotal)

    numberOfInvoices += 1
    totalOfInvoices += invoiceTotal
    invoiceAverage = totalOfInvoices / numberOfInvoices

    For i = 0 To numberOfInvoices - 1
        array(i) = i
        ReDim Preserve array(0 To numberOfInvoices - 1)
    Next i

    If numberOfInvoices = 1 Then
        minimum = totalOfInvoices
        middle = totalOfInvoices
        maximum = totalOfInvoices
    End If

    If numberOfInvoices > 1 Then
        midpoint = numberOfInvoices / 2
        middle = array.GetValue(midpoint)
        For Each i In array
            If i < UBound(array) - 1 Then If array(i) > array(i + 1) Then maximum = array(i)
            If i < UBound(array) - 1 Then If array(i) < array(i + 1) Then minimum = array(i)
        Next i
    End If

    txtLargestInvoice.Text = maximum.ToString
    txtSmallestInvoice.Text = minimum.ToString
    txtMidPoint.Text = middle.ToString
    txtNumberOfInvoices.Text = numberOfInvoices.ToString
    txtTotalOfInvoices.Text = FormatCurrency(totalOfInvoices)
    txtInvoiceAverage.Text = FormatCurrency(invoiceAverage)

    txtEnterSubtotal.Text = ""
    txtEnterSubtotal.Select()
End Sub

Private Sub BtnClearTotals_Click(sender As Object,
        e As EventArgs) Handles btnClearTotals.Click
    numberOfInvoices = 0
    totalOfInvoices = 0
    invoiceAverage = 0

    txtNumberOfInvoices.Text = ""
    txtTotalOfInvoices.Text = ""
    txtInvoiceAverage.Text = ""

    txtEnterSubtotal.Select()
End Sub

Private Sub BtnExit_Click(sender As Object,
        e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

在线评论和解释

Private Sub OPCode2()
        'You can declare and initialize an array in one line
        Dim arr() As Double = {13.5, 18.0, 6.2, 11.8, 13.6}
        'Just check intellisense for a list of methods and properties of arrays
        Dim myAverage As Double = arr.Average()
        Dim myMin As Double = arr.Min
        Dim myMax As Double = arr.Max
        Dim myMedian As Double
        'The original array is lost when .Sort is applied
        Array.Sort(arr)
        Dim myLength As Integer = arr.Length
        'If the lenght of the array is even you could also average 
        'the middle two values And return that number which
        'may not be a value in the original array
        myMedian = arr(CInt(myLength / 2)) 'returns the higher of the middle two if length is even
        'or the actual middle value in the sorted array
        MessageBox.Show($"        The Average value is {myAverage}
        The Maximum value is {myMax}
        The Minimum value is {myMin}
        The Median value is {myMedian}")
    End Sub
相关问题