Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 基于数组计算平均值_Vb.net_Average - Fatal编程技术网

Vb.net 基于数组计算平均值

Vb.net 基于数组计算平均值,vb.net,average,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.Tri

我试图计算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
根据上述信息,仅添加前2项(
tbAdjCostPerGal
),然后求平均值,因为第3-6项的值为0,最终结果为每加仑平均成本2.364美元

总之,我需要求出所有
tbAdjCostPerGal.text>0
的总和和平均值。我相信,如果我只对
tbAdjCostPerGal
条目取平均值,这将给出我想要的答案。每个记录将始终有6个条目


我知道我需要创建一种方法来创建一个变量来查看
tbAdjCostPerGal
条目,只合计那些值大于0的条目,这就是我缺乏知识的地方。有人能帮我把这封信的格式弄好吗?

我想你需要的是总价除以加仑数。例如:

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)
Dim数组={af1,af2,af3,af4,af5,af6}
Dim count=array.count(函数(d)d 0)
Dim average=IIF(count=0,0,array.Sum()/count)
或过滤掉0值:

tbFuelCostAdj.Text = array.Where(Function(d) d <> 0).Average.ToString("C3")
tbFuelCostAdj.Text=array.Where(函数(d)d0).Average.ToString(“C3”)

0是一个合法的值-为什么它不起作用?看起来这是平均燃油价格。我猜燃料的价格为$0.0意味着没有价格记录,而燃料是免费的。(我必须去哪里加注燃油??)因此,在计算平均燃油成本时,您确实希望排除$0值。在这种情况下,应该使用更好的“无价格记录”信号量。但因为不是这样,我们必须使用$0.0的信号量。你的猜测是正确的。是的,0.000或0.000美元将意味着没有价格或燃料在该行购买。否则,像你一样,我每天都会在那里买燃料!!默认值始终为0,但无法计算,因为那样的话,除法器将始终为6,这就是我失败的地方。我只需要将它除以值大于0的数字就可以了。除了一个例外,这很好用。。。如果记录没有购买任何燃料,那么代码将崩溃。。。除以零误差。因此,在计算之前,需要进行检查以确保存在某些内容。我尝试在try语句中添加:如果af1>0,则。。。。但这并没有什么区别,仍然会得到被零除的错误。@JohnEtling所以在dividingre处理TRY语句之前检查除数是否为0,问题就解决了。谢谢斯莱的决心。