根据数组

时间:2017-05-14 23:42:03

标签: vb.net average

我正在尝试计算6个项目的总体平均值。其中一些项目可能为零,因此在计算平均值时不会发挥作用。平均完全让我感到困惑,并且很难解决这个问题。

我目前拥有的代码:

     Dim a,
        af1, af2, af3, af4, af5, af6,
        t7, t8, t9, t10, t11, t12 As Decimal

        'Adjusted Fuel Cost per line
    af1 = CDec(IIf(tbAdjCostPerGal1.Text.Trim = "", 0D, tbAdjCostPerGal1.Text.Trim))
    af2 = CDec(IIf(tbAdjCostPerGal2.Text.Trim = "", 0D, tbAdjCostPerGal2.Text.Trim))
    af3 = CDec(IIf(tbAdjCostPerGal3.Text.Trim = "", 0D, tbAdjCostPerGal3.Text.Trim))
    af4 = CDec(IIf(tbAdjCostPerGal4.Text.Trim = "", 0D, tbAdjCostPerGal4.Text.Trim))
    af5 = CDec(IIf(tbAdjCostPerGal5.Text.Trim = "", 0D, tbAdjCostPerGal5.Text.Trim))
    af6 = CDec(IIf(tbAdjCostPerGal6.Text.Trim = "", 0D, tbAdjCostPerGal6.Text.Trim))

            'Truck Gallons Purchased Related
    t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim))
    t8 = CDec(IIf(tbTrkGalsPurch2.Text.Trim = "", 0D, tbTrkGalsPurch2.Text.Trim))
    t9 = CDec(IIf(tbTrkGalsPurch3.Text.Trim = "", 0D, tbTrkGalsPurch3.Text.Trim))
    t10 = CDec(IIf(tbTrkGalsPurch4.Text.Trim = "", 0D, tbTrkGalsPurch4.Text.Trim))
    t11 = CDec(IIf(tbTrkGalsPurch5.Text.Trim = "", 0D, tbTrkGalsPurch5.Text.Trim))
    t12 = CDec(IIf(tbTrkGalsPurch6.Text.Trim = "", 0D, tbTrkGalsPurch6.Text.Trim))

            'Calculate ADJUSTED Average Cost of ALL Fuel plus Fuel Card fee and any Fuel Discounts this Load
    Try
        If af1 > 0 Then
            a = (af1 + af2 + af3 + af4 + af5 + af6) / (t7 + t8 + t9 + t10 + t11 + t12)
            tbFuelCostAdj.Text = a.ToString("C3")
        Else
            a = 0D
            tbFuelCostAdj.Text = a.ToString("C3")
        End If
    Catch ex As Exception
        'If a calculation error occurs, show Error message box
        Dim frm As New MeMsgCalcError(ex, "'Adjusted Cost of Fuel this Load' Calculation Error." & vbCrLf & "Lines 644-652")
        frm.Show()
    End Try

这是我的代码的相关部分,我试图获得平均值。

我举个例子:

  • tbAdjCostPerGal1 = $ 2.259
  • tbAdjCostPerGal2 = $ 2.469
  • tbAdjCostPerGal3 = $ 0.000
  • tbAdjCostPerGal4 = $ 0.000
  • tbAdjCostPerGal5 = $ 0.000
  • tbAdjCostPerGal6 = $ 0.000

  • tbTrkGalsPurch1 = 100.000

  • tbTrkGalsPurch2 = 93.000
  • tbTrkGalsPurch3 = 0.000
  • tbTrkGalsPurch4 = 0.000
  • tbTrkGalsPurch5 = 0.000
  • tbTrkGalsPurch6 = 0.000

根据上述信息,只会添加前两个条目(tbAdjCostPerGal),然后进行平均,因为第3-6项为0值,最终结果为每加仑平均成本$ 2.364。

总而言之,我需要总计和平均所有tbAdjCostPerGal.text > 0。我相信如果我只对tbAdjCostPerGal条目求平均值,这会给我一些我想要的答案。每条记录总会有6个条目。

我知道我需要创建一些方法来创建一个变量来查看tbAdjCostPerGal条目,总计只有那些值大于0的条目,这是我缺乏知识的地方。有人可以协助我正确格式化吗?

1 个答案:

答案 0 :(得分:1)

我猜你需要总价除以加仑数。例如:

Total Price = $2.259 * 100.000 + $2.469 * 93.000 + $0.000 * 0.000 + ... = $455.517

Average per Gallon = Total Price / # of Gallons = 455.517 / (100 + 93 + ...) = 2.36019170984

对于非0的平均值,您可以将总和除以不为0的值的数量:

Dim array = { af1, af2, af3, af4, af5, af6 }

Dim count = array.Count(Function(d) d <> 0)

Dim average = IIF(count = 0, 0, array.Sum() / count)

或过滤掉0值:

tbFuelCostAdj.Text = array.Where(Function(d) d <> 0).Average.ToString("C3")