我是VB的新手,我被困在家庭作业上

时间:2013-03-11 17:20:41

标签: arrays vb.net

我必须在学生姓名和成绩的文件中阅读,并将每个类别分成一个单独的数组,例如一个数组中的所有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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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

答案 2 :(得分: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