简体   繁体   English

在vb.net中将成绩添加到文本文件

[英]Adding grades to a text file in vb.net

Hello I'm trying to add new grades to a text file using the button btnRECORD entered in the textbox txtRECORD. 您好,我正在尝试使用在文本框txtRECORD中输入的按钮btnRECORD将新成绩添加到文本文件中。 I'm using the button btnDISPLAY to calculate the average of the grades and the number of grades above average and display them in a listbox. 我正在使用按钮btnDISPLAY来计算成绩的平均值和高于平均值的成绩数,并将其显示在列表框中。 But I can't seem to add new grades to the textfile with btnRECORD. 但是我似乎无法使用btnRECORD向文本文件添加新成绩。 (the textfile is empty) (文本文件为空)

  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

Oops your C# is showing :). 糟糕,您的C#正在显示:)。 Comments in VB are signified with a ' VB中的注释以'

Your main problem appears to be casting an array of double to string. 您的主要问题似乎是将双精度数组转换为字符串。 In the WriteAllLines method. 在WriteAllLines方法中。 Something like this should work better: 这样的事情应该更好地工作:

    IO.File.WriteAllLines("Exam.txt", (From d In grades
                                             Let str As String = d.ToString
                                             Select str).ToArray)

Another thing I noticed, array indexes are integer but one of your for loops is declared with a double. 我注意到的另一件事,数组索引是整数,但是其中一个for循环声明为double。

To use the return type of a function you should explicitly declare that type so that the compiler can recognize it. 要使用函数的返回类型,应显式声明该类型,以便编译器可以识别它。

 Function avg(ByVal average As Double, ByVal aboveAverage As Integer) As Integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM