您好我正在尝试使用在文本框txtRECORD中输入的按钮btnRECORD将新成绩添加到文本文件中。我正在使用按钮btnDISPLAY来计算成绩的平均值和高于平均水平的成绩数,并将其显示在列表框中。但我似乎无法使用btnRECORD为文本文件添加新的成绩。 (文本文件为空)
Public Class frmGRADES
Dim temp() As String = IO.File.ReadAllLines("Exam.txt")
Dim grades(temp.Length - 1) As Double
Dim average As Double
Dim aboveAverage As Integer
Function avg(ByVal average As Double, ByVal aboveAverage As Integer)//function for average
For i As Double = 0 To grades.Length - 1
If grades(i) > average Then
aboveAverage = aboveAverage + 1
End If
Next
Return aboveAverage
End Function
Private Sub btnRECORD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRECORD.Click
ReDim Preserve grades(temp.Length - 1)
grades(temp.Length - 1) = CDbl(txtRECORD.Text)
IO.File.WriteAllLines("Exam.txt", grades) //write grades to textfile
aboveAverage = 0
End Sub
Private Sub btnDISPLAY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDISPLAY.Click
lstRESULTS.Items.Clear()
For i As Integer = 0 To grades.Length - 1
grades(i) = CDbl(temp(i))
Next
average = grades.Average
lstRESULTS.Items.Add("The Average Score is: " & average)
aboveAverage = avg(average, aboveAverage)
lstRESULTS.Items.Add("Number of Grades above average is: " & aboveAverage)
aboveAverage = 0
End Sub
End Class
答案 0 :(得分:0)
哎呀你的C#正在显示:)。 VB中的注释用'
您的主要问题似乎是将一个double数组转换为字符串。在WriteAllLines方法中。这样的事情会更好:
IO.File.WriteAllLines("Exam.txt", (From d In grades
Let str As String = d.ToString
Select str).ToArray)
我注意到的另一件事是,数组索引是整数,但是你的一个for循环是用double声明的。
要使用函数的返回类型,您应该显式声明该类型,以便编译器可以识别它。
Function avg(ByVal average As Double, ByVal aboveAverage As Integer) As Integer