vb.net 如何计算数组的平均值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37272389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:53:41  来源:igfitidea点击:

How do I calculate the average of an array?

vb.net

提问by Modrisco

 Public Class Form1
Dim x As Integer
Dim y(9) As Double
Dim average As Double
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For x = 0 To 9
        y(x) = x
    Next
    average = y(9) / 10
    Label1.Text = average
End Sub
End Class

I'm having trouble trying to calculate the average of 0 to 9 array, I realize that my code is only going to calculate 9 divided by 10, How do I proceed with calculating the average of the 10 numbers?

我在尝试计算 0 到 9 数组的平均值时遇到问题,我意识到我的代码只会计算 9 除以 10,我该如何继续计算 10 个数字的平均值?

回答by Michael Z.

Use Linq...

使用林克...

Dim average As Double = y.Average()

Another thing you can do with Linq is exclude certain values from your average. Take this example that sets 9 items in myNumbers()to 10 and then sets the 10th one to zero. I use Linq to first filter for numbers greater than zero then I perform the Averageaggregate.

您可以使用 Linq 做的另一件事是从平均值中排除某些值。以这个例子为例,将 9 项设置myNumbers()为 10,然后将第 10项设置为零。我使用 Linq 首先过滤大于零的数字,然后执行Average聚合。

Dim myNumbers(9) As Double
For i As Integer = 0 To 8 Step 1
    myNumbers(i) = 10
Next i

myNumbers(9) = 0

Dim average As Double = myNumbers.Where(Function(num) num > 0).Average()
'Optionally, you could also do it this way:
Dim average2 As Double = (From num In myNumbers
                          Where num > 0).Average()
MessageBox.Show(average)

It gets even better when you use classes. Consider this class Thing:

当你使用类时它会变得更好。考虑这个类Thing

Public Class Thing
 Public Name As String = ""
 Public Number As Double = 0

 Public Sub New (name As String, number As Double)
    Me.Name= name
    Me.Number = number
 End Sub
End Class 

I can create a collection of that class and the .Wherebecomes even more powerful. In this example I'm averaging Thing.Numberby Thing.Name

我可以创建该类的集合,并且.Where变得更加强大。在这个例子中,我均Thing.Number通过Thing.Name

Dim things As List(Of Thing) = New List(Of Thing)()
things.Add(New Thing("Michael", 10))
things.Add(New Thing("Michael", 5))
things.Add(New Thing("Michael", 7))
things.Add(New Thing("Michael", 9))
things.Add(New Thing("Rian", 10))
things.Add(New Thing("Rian", 10))
things.Add(New Thing("Rian", 10))

Dim AverageMichael As Double = things.Where(Function(thing) thing.Name = "Michael").Average(Function(thing) thing.Number)
Dim AverageRian As Double = things.Where(Function(thing) thing.Name = "Rian").Average(Function(thing) thing.Number)

MessageBox.Show(AverageMichael)
MessageBox.Show(AverageRian)

A side note
If you're a .NET developer and you'd like to learn to use Linq or you simply need an easy environment to test .NET code then I highly recommend LinqPad. I used LinqPad for all the code in this post.

附注
如果您是 .NET 开发人员并且您想学习使用 Linq 或者您只需要一个简单的环境来测试 .NET 代码,那么我强烈推荐 LinqPad。我在这篇文章中的所有代码都使用了 LinqPad。

http://www.linqpad.net/

http://www.linqpad.net/

回答by bam500

The average of a set of values is equals to the sum of values divided by the number of values. You need to declare a variable sumto add every value of your array and then divide by 10.

一组值的平均值等于值的总和除以值的数量。您需要声明一个变量sum来添加数组的每个值,然后除以 10。

回答by Aimnox

You need to sum all the values and then divide. I recomend using a simple function for it:

您需要对所有值求和然后进行除法。我建议使用一个简单的函数:

Function getAverage(y As Double()) As Double
    Dim z As Double
    For Each i As Double In y
        z += i
    Next
    Return z / y.Length
End Function

Just set the average to getAverage(y)

只需将平均值设置为 getAverage(y)