【发布时间】:2016-04-29 11:24:25
【问题描述】:
我可笑地坚持这一点。我下面的代码总结了文本文件Dailyfile 中的所有数字,并将总数输出到AverageFile。问题是我不想总结。我希望它找出所有数字中的average。
我该怎么做?
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()。