我已经在这个VB项目上工作了一个多月,但仍然陷入困境。我需要从数据文件中读取一系列数字然后对它们进行平均。有0个分隔系列,所以我有2个if语句对它进行排序,但我不断收到这个NaN错误。
Dim sum As Integer
Dim count As Integer
Dim TotalSum As Integer
Dim TotalCount As Integer
Dim Average As Double
Dim TotalAverage As Double
Dim seriescount As Double
Dim i As Integer
Dim intarray(i) As Double
Dim srRead As IO.StreamReader = New IO.StreamReader("NumberData.txt")
i = srRead.ReadLine()
Do While i < count
If intarray(i) <> 0 Then
sum += intarray(i)
seriescount += 1
Else
Average = sum / seriescount
lstNumbers.Items.Add(Average)
sum = 0
seriescount = 0
TotalSum += Average
TotalCount += 1
End If
i = i + 1
Loop
TotalAverage = TotalSum / TotalCount
lstNumbers.Items.Add(TotalAverage)
请帮助
答案 0 :(得分:0)
您的代码包含多个错误。您将数据存储为单个整数,然后使用该整数,如果它是索引。我修复了我能做的事情,但我建议你开始在未来的项目中使用严格模式,直到你理解了类型转换是如何工作的。
这应该有用,或者至少可以让您清楚地了解代码中的错误
Dim sum, count, TotalSum, TotalCount As Integer
Dim Average, TotalAverage , seriescount as double
Dim srRead As IO.StreamReader = New IO.StreamReader("NumberData.txt")
dim content as string = srRead.ReadLine()
for i as integer = 0 to content.lenght-1
If content.chars(i) <> "0" Then
sum += content.chars(i) //cint needed if you use strict
seriescount += 1
Else
TotalSum += sum
TotalCount += seriescount
Average = sum / seriescount
lstNumbers.Items.Add(Average)
sum = 0
seriescount = 0
End If
Loop
TotalAverage = TotalSum / TotalCount
lstNumbers.Items.Add(TotalAverage)