[英]How do I display and calculate average ? And how do I write the value to the text file only once?
[英]How do I calculate the average
我能夠在 dataGridView 中獲得總和,但無法正確獲得平均值。
foreach (DataGridViewRow row in dataGridView.Rows) {
row.Cells["Average"].Value = Convert.ToDouble(row.Cells[4].Value) +
Convert.ToDouble(row.Cells[5].Value) +
Convert.ToDouble(row.Cells[6].Value);
}
平均值是元素的總和除以元素總數。 在您的情況下,由於共有三個元素,因此您必須將總和除以三。
您不是在進行平均計算,而是在進行求和計算。 只需將總數除以提供的值數即可得出平均值:
foreach (DataGridViewRow row in dataGridView.Rows)
row.Cells["Average"].Value = (Convert.ToDouble(row.Cells[4].Value) + Convert.ToDouble(row.Cells[5].Value) + Convert.ToDouble(row.Cells[6].Value)) / 3;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.