我在这个问题上卡住了。下面的代码将文本文件Dailyfile
中所有数字相加,并将总和输出到AverageFile
。问题是我不想让它相加,我想找出所有数字的平均值
。
我该怎么办?
Dim AverageFile As String = "C:\xxx\zzz\" & System.DateTime.Now.ToString("yyyyMMdd") & ".txt"
Dim DailyFile As String = "C:\xxx\xxx\" & System.DateTime.Now.ToString("yyyyMMdd") & ".txt"
Try
If System.IO.File.Exists(AverageFile) Then
Dim total As double = 0
For Each line As String In IO.File.ReadAllLines(DailyFile)
total += Double.Parse(line)
Next
Dim objWriter As New System.IO.StreamWriter(AverageFile, false)
objWriter.WriteLine(total.ToString)
objWriter.Close()
Else
'Nothing yet
End If
Catch ex As Exception
lbErrors.Items.Add(String.Concat(TimeOfDay & " Error 98: File or folder might not exist. Restart application... ", ex.Message))
End Try
Dailyfile
就像这样:
我已经尝试了很多 total 0= double.parse(line)
的变化,因为我感觉问题就在那里。我也试过 diming the total as integer = 0
。但我刚刚开始学习计算,所以不太清楚具体情况。
File.ReadAllLines(path).Select(double.Parse).Average()
。 - Enigmativity