0

每当我尝试运行我的程序时,我都会收到一个错误消息

“从字符串“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
4

1 回答 1

0

您需要在文本文件中每行有一个分数。

于 2013-08-02T01:58:09.230 回答