1

我必须读入学生姓名和成绩的文件,并将每个类别放入一个单独的数组中,例如一个数组中的所有 homework1 成绩,另一个数组中的 homework2 成绩等,但我什至不知道该怎么做。到目前为止,这是我的代码,我确信其中大部分是错误的,并且我有错误消息。

Public Class frmGrades

    Dim FirstName As Array
    Dim LastName As Array
    Dim HW1 As Array
    Dim HW2 As Array
    Dim Project As Array
    Dim Midterm As Array
    Dim Final As Array
    Dim Average As Array
    Dim record, fields(7) As String
    Dim inFile As StreamReader
    Dim outFile As StreamWriter



    Private Sub cmdReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReport.Click
        MsgBox("Summary report has been created", MsgBoxStyle.OkOnly, "Grades")
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub

    Private Sub frmGrades_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        inFile = File.OpenText("Grades.csv")
        outFile = File.CreateText("Report.csv")

        Do While (inFile.Peek <> -1)
            record = inFile.ReadLine
            fields = Split(record, ",")
            FirstName = CInt(fields(0))
            LastName = fields(1)
            HW1 = fields(2)
            HW2 = fields(3)
            Project = fields(4)
            Midterm = fields(5)
            Final = fields(6)
            'Average = (HW1 * 0.0833) + (HW2 * 0.0833) + (Project * 0.1666) + (Midterm * 0.3332) + (Final * 0.3332)
            outFile.Write(FirstName)
            outFile.Write(LastName)
            outFile.Write(HW1)
            outFile.Write(HW2)
            outFile.Write(Project)
            outFile.Write(Midterm)
            outFile.Write(Final)
            outFile.WriteLine(Average)
        Loop

    End Sub


    Private Function Split(ByVal record As String, ByVal p2 As String) As String()
        Throw New NotImplementedException
    End Function




End Class
4

3 回答 3

1

由于数组是将多个值收集到单个变量中的一种方式,因此您希望将值添加到适当的数组中。您的 while 循环中的代码(如果有的话)是用字段中的值替换每个整个数组 - 实际上,这是错误消息的来源:您正在尝试将字符串放入声明为大批。

于 2013-03-11T17:26:39.353 回答
1

我想你的教授可能会告诉你这样做,

 Dim cnt as integer=0

 Do While (inFile.Peek <> -1)

            record = inFile.ReadLine
            fields = Split(record, ",")
            FirstName(cnt) = CInt(fields(0))
            LastName(cnt) = fields(1)
            HW1(cnt) = fields(2)
            HW2(cnt) = fields(3)
            Project(cnt) = fields(4)
            Midterm(cnt) = fields(5)
            Final(cnt) = fields(6)

            cnt += 1
           ..............
           ..............
 Loop
于 2013-03-11T17:38:36.973 回答
0

FirstName、LastName 等是字符串数组,我认为这是混淆的重点。小心不理解就提交这个作业,并考虑对你不理解的东西做一些改变。

Dim FirstName() As String
Dim LastName() As String
Dim HW1() As Single
Dim HW2() As Single
Dim Project() As Single
Dim Midterm() As Single
Dim Final() As Single
Dim Average() As Single

Dim fields() As String
Dim inFile As StreamReader
Dim outFile As StreamWriter

Private Sub frmGrades_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    inFile = File.OpenText("Grades.csv")
    outFile = File.CreateText("Report.csv")
    Dim n As Integer = 0
    Dim seperator As String = ", "
    Do While (inFile.Peek <> -1)
        ' read the fields from the current line in the file
        fields = inFile.ReadLine.Split(",")
        ' redim all the arrays to the appropriate size
        ReDim Preserve FirstName(n)
        ReDim Preserve LastName(n)
        ReDim Preserve HW1(n)
        ReDim Preserve HW2(n)
        ReDim Preserve Project(n)
        ReDim Preserve Midterm(n)
        ReDim Preserve Final(n)
        ReDim Preserve Average(n)
        ' set the values from the fields
        FirstName(n) = fields(0)
        LastName(n) = fields(1)
        HW1(n) = Single.Parse(fields(2))
        HW2(n) = Single.Parse(fields(3))
        Project(n) = Single.Parse(fields(4))
        Midterm(n) = Single.Parse(fields(5))
        Final(n) = Single.Parse(fields(6))
        ' calculate the average
        'Average = (HW1 * 0.0833) + (HW2 * 0.0833) + (Project * 0.1666) + (Midterm * 0.3332) + (Final * 0.3332)
        Average(n) = Single.Parse(
            (HW1(n) * 1 / 12) +
            (HW2(n) * 1 / 12) +
            (Project(n) * 1 / 6) +
            (Midterm(n) * 2 / 6) +
            (Final(n) * 2 / 6))
        ' write a line to the output file
        outFile.WriteLine(FirstName(n) & seperator & LastName(n) & seperator &
                      HW1(n).ToString() & seperator & HW2(n).ToString() & seperator &
                      Project(n).ToString & seperator & Midterm(n).ToString() & seperator &
                      Final(n).ToString() & seperator & Average(n).ToString())
        n += 1
    Loop
    inFile.Close()
    outFile.Close()
End Sub
于 2013-03-11T18:29:40.180 回答