简体   繁体   English

从 CSV 文件 C# 输出姓名和考试成绩

[英]Outputting names and test scores from a CSV File C#

I have a project which I have created that reads from a CSV File which contains names and scores of students in a class.我创建了一个项目,该项目从 CSV 文件中读取,该文件包含 class 中学生的姓名和分数。 The program is supposed to read from the CSV File and then output all of the names.该程序应该从 CSV 文件中读取,然后从 output 中读取所有名称。 I have created the program and it works, it's just that I can only output the name and scores of 1 student in the class when it is meant to be for 12. If anyone could show me where I need to change my code in order to get the output for the whole class it would be very much appreciated.我已经创建了该程序并且它可以工作,只是我只能 output class 中的 1 名学生的姓名和分数,当它是 12 时。如果有人能告诉我我需要在哪里更改我的代码以便获得整个 class 的 output 将不胜感激。 Thanks.谢谢。 CSV 文件

    public static void Main(string[] args)
    {
        string[] names = loadNames();
        int[] marks = loadMarks();

        outputMarks(names, marks);
        Console.ReadKey();
    }
    static string[] loadNames()
    {
        string filename = @"testMarks.csv";
        string[] tempData;
        string[] tempNames = new string[2];


        using (StreamReader currentfile = new StreamReader(filename))
        {
            tempData = currentfile.ReadLine().Split(',');
        }

        tempNames[0] = tempData[0];
        tempNames[1] = tempData[1];

        return tempNames;
    }
    static int[] loadMarks()
    {
        string filename = @"testMarks.csv";
        string[] tempData;


        using (StreamReader currentfile = new StreamReader(filename))
        {
            tempData = currentfile.ReadLine().Split(',');
        }
        int[] tempMarks = new int[tempData.Length - 2];

        for (int i = 2; i < tempData.Length; i++)
        {
            tempMarks[i - 2] = int.Parse(tempData[i]);
        }
        return tempMarks;
    }
    static void outputMarks(string[] names, int[] marks)
    {
        for (int i = 0; i < 2; i++)
        {
            Console.Write(names[i] + "\t");
        }

        for (int i = 0; i < 6; i++)
        {
            Console.Write(marks[i] + "\t");
        }
        Console.WriteLine();
    }
}

} }

Assuming the content of the testMarks.csv is like this (without empty lines ofcourse):假设 testMarks.csv 的内容是这样的(当然没有空行):

John,Adams,100,79,77,83,55,37约翰,亚当斯,100,79,77,83,55,37

Sarah,Baxter,99,66,78,57,62,70莎拉,巴克斯特,99,66,78,57,62,70

Jane,Clague,100,100,61,95,59,38简,克拉格,100,100,61,95,59,38

I would do this like:我会这样做:

public static void Main(string[] args)
{
    var allStudentsAndNotes = LoadStudentsAndNotes();
    Console.ReadKey();
}

  private List<Student> LoadStudentsAndNotes()
    {

        string pth = @"C:\MyDesktopPath\testMarks.csv";
        
        var lines = System.IO.File.ReadAllLines(pth);  

        var studentList = new List<Student>();
        Student student;
        foreach (var line in lines)
        {
            student = new Student();
            var datas = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            student.FirstName = datas[0];
            student.LastName = datas[1];
            for (int i = 2; i < datas.Length; i++)
            {
                student.Notes.Add(int.Parse(datas[i]));
            }

            studentList.Add(student);
        }
        return studentList;
    }

A little class一点class

class Student
{
    public Student()
    {
        Notes = new List<int>();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<int> Notes { get; set; }

}

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

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