每当我尝试运行程序时,我都会收到错误消息
“从字符串”59 60 65 75“转换为”Double“类型无效。”
我从一个名为scores.txt的文件中获取数据,该文件包含整数59,60,65,75。我不太确定如何解决这个问题,VB提出了一些建议,例如确保值小于无穷大(显然是这样)并确保源类型可转换为目标类型。(这个我不确定如何调试)任何建议?这是我的代码
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnanalyze.Click
Dim scores() As String =
IO.File.ReadAllLines("scores.txt")
Dim intdata(scores.length - 1) As Double 'declare an array of type double to store the data of text file
Dim mean As Double
Dim sdeviation As Double = 0
For i As Integer = 0 To scores.length - 1
intdata(i) = CDbl(scores(i))
Next
mean = intdata.Average 'mean is the average of the numbers in the collection
For j As Integer = 0 To intdata.Length - 1
sdeviation = +Math.Pow(intdata(j) - mean, 2)
Next
sdeviation = sdeviation / (intdata.Length)
sdeviation = Math.Sqrt(sdeviation)
lblmean.text = FormatNumber(mean)
lblsd.text = FormatNumber(sdeviation)
lblnumofexams.text = scores.length
Dim query = From score In scores
Let Sscore = score
Let grade = getGrade(score, mean, sdeviation)
Select Sscore, grade
dvgoutput.datasource = query.tolist
dvgoutput.currentcell = Nothing
dvgoutput.columns("Sscore").headertext = "score"
dvgoutput.columns("grade").headertext = "grade"
End Sub
Public Function getGrade(ByVal ES, ByVal m, ByVal s) As String
If ES >= m + (1.5 * s) Then
Return "A"
End If
If m + (0.5 * s) < +ES And ES < m + (1.5 * s) Then
Return "B"
End If
If m - (0.5 * s) <= ES And ES < m + (0.5 * s) Then
Return "C"
End If
If m - (1.5 * s) <= ES And ES < +m - (0.5 * s) Then
Return "D"
End If
If ES < m - (1.5 * s) Then
Return "F"
End If
Return ""
End Function
End Class
答案 0 :(得分:0)
您需要在文本文件中每行获得一个分数。