每當我嘗試運行我的節目,我不斷收到,「從字符串轉換‘59 60 65 75個國家轉換字符串錯誤
錯誤’鍵入‘雙師型’是無效的。」
我從一個名爲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
我在這條線上得到一個錯誤「intdata(i)= CDbl(scores(i))」 – user2596437
這聽起來像是在讀所有的數字,而不僅僅是一個。因此無法將其轉換。弄清楚如何一次輸入一個數字。 – Jiminion
如果您在文本文件的每一行都有一個分數,該怎麼辦? – Jiminion