将CSV文件值存储在3个单独的列表中

时间:2019-11-27 05:37:50

标签: c#

我创建了一个.csv文件用于琐事游戏,该文件存储一个问题,每个问题的答案和1-3分。我想将问题存储在字符串列表中,将答案存储在字符串列表中,并将点存储在int列表中。然后,我想一次向用户显示每个问题,答案和分数。我不确定该怎么做。

任何帮助将不胜感激。

这是.csv文件代码

static void Main(string[] args)
{
        // The path of the file to write to.
        string filename = "C:\\Intel\\Trivia.csv";

        // Parallel array of related trivia data
        string[] questions = { "Question1", "Question2", "Question3", "Question4", "Question5","Question6", "Question7","Question8","Question9","Question10", };
        string[] answers = { "A1", "A2", "A3", "A4", "A5", "A6", "A7","A8","A9","A10" };
        int[] points = { 1, 3, 3, 1, 2,1,2,1,3,1};

        try
        {
            // Write each element of the parallel array to a file.
            using (StreamWriter writer = new StreamWriter(filename))
            {
                for (int index = 0; index < questions.Length; index++)
                {
                    writer.WriteLine(questions[index] + ","
                        + answers[index] + ","

                        + points[index]
                        );
                }
                Console.WriteLine("Data successfully written to " + filename);
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be written.");
            Console.WriteLine(e.Message);
        }

    }
}

1 个答案:

答案 0 :(得分:2)

在我看来,这确实不是解决问题的方法。它可以工作,但是非常脆弱,因为您必须处理三个阵列并使它们完美同步。最好只创建一个类并保留一个对象数组:

void Main()
{
    // The path of the file to write to.
    string filename = "D:\\temp\\Trivia.txt";

    var questions = new List<QuestionInfo>() {
        new QuestionInfo("Q1", "A1", 1),
        new QuestionInfo("Q2", "A2", 3),
        new QuestionInfo("Q3", "A3", 3),
        new QuestionInfo("Q4", "A4", 1),
        new QuestionInfo("Q5", "A5", 2),
        new QuestionInfo("Q6", "A6", 1),
        new QuestionInfo("Q7", "A7", 2),
        new QuestionInfo("Q8", "A8", 1),
        new QuestionInfo("Q9", "A9", 3),
        new QuestionInfo("Q10", "A10", 1),
    };
    using (TextWriter tw = new StreamWriter(filename))
    {
        try
        {
            QuestionInfo.WriteCsv(questions, tw);
            Console.WriteLine("Data successfully written to " + filename);
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be written.");
            Console.WriteLine(e.Message);
        }
    }
}

public class QuestionInfo
{
    string Question { get; }
    string Answer { get; }
    int Points { get; }

    public QuestionInfo(string question, string answer, int points)
    {
        Question = question;
        Answer = answer;
        Points = points;
    }

    public void WriteCsv(TextWriter tw)
    {
        tw.WriteLine($"{Question},{Answer},{Points}");
    }

    public static void WriteCsv(List<QuestionInfo> questions, TextWriter tw)
    {
        foreach (var question in questions)
        {
            question.WriteCsv(tw);
        }
    }
}

现在,您有了一个问题信息对象数组,该对象将单个问题的所有信息都放在一个位置,而不是在三个数组之间隔开。实际上,有很多可以很好地处理CSV文件的nuget程序包,我肯定会考虑使用其中的一个,但是在这样的简单情况下,自己进行处理并不重要。