1
的陣列的平均我有此代碼查找整數
Dim intPerson As Integer
For Each intPerson In intAge
Next
intPerson每次執行循環,因爲INTAGE是一個數組時間持有不同的數字。有沒有一種方法可以通過每次添加每個數字然後除以數組的數量來找到intPerson的平均值?
的陣列的平均我有此代碼查找整數
Dim intPerson As Integer
For Each intPerson In intAge
Next
intPerson每次執行循環,因爲INTAGE是一個數組時間持有不同的數字。有沒有一種方法可以通過每次添加每個數字然後除以數組的數量來找到intPerson的平均值?
最簡單的方法是使用Linq:
'create an array with some sample ages
Dim intAge As Integer() = {22, 34, 56, 87, 19}
'find the average
Dim averageAge = intAge.Average 'averageAge = 43.6
如果wanto做速記,你可以用數字總結的價值和鴻溝:
Dim totalAges As Integer = 0
For i As Integer = 0 To intAge.Count - 1
totalAges += intAge(i)
Next
averageAge = totalAges/intAge.Count 'averageAge = 43.6
你不需要標註迭代器在'For Each'循環中。 –
是的。我的大腦正在變成馬鈴薯。我應該輸入的內容是:「你不需要事先對'For Each'循環的迭代器進行調整。」 –