1

生徒の名前と成績のファイルを読み込んで、各カテゴリを別の配列に入れる必要があります。たとえば、すべての宿題 1 の成績を 1 つの配列に、宿題 2 の成績を別の配列に入れるなどですが、これを行う方法さえわかりません。これはこれまでの私のコードであり、そのほとんどが間違っていると確信しており、エラー メッセージが表示されます。

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

先生がこうしろと言ったのかもしれませんが、

 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 に答える
1

配列は複数の値を 1 つの変数にまとめる 1 つの方法であるため、適切な配列に値を追加する必要があります。while ループのコードは、どちらかといえば、各配列全体をフィールドの値に置き換えています。実際、これがエラー メッセージの原因です。配列。

于 2013-03-11T17:26:39.353 に答える
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 に答える