C#文件流 - 构建一个测验

Sar*_*rit 2 c# csv text-files

尝试使用外部文件(txt或CSV)以便在C#中创建文件流.文件中的数据是由以下内容构成的测验游戏:

1个简短问题4个可能的答案1个正确答案

该程序应该能够告诉用户他是否正确回答.

我正在寻找一个示例代码/算法/教程,介绍如何使用外部文件中的数据在C#中创建一个简单的测验.此外,有关如何构建txt文件的任何建议(如何将答案标记为正确答案?).有什么建议或链接吗?谢谢,

Cer*_*rus 12

如果您必须从文件(而不是从数据库)加载数据,我的建议是使用XML文件.

使用文本文件需要您非常清楚地定义问题的各个元素的结构.使用CSV可以工作,但你必须定义一种方法来逃避问题或答案本身的逗号.这可能会使问题复杂化.

因此,重申一下,恕我直言,XML是存储此类数据的最佳方式.这是一个简短的示例,演示了您可能使用的可能结构:

<?xml version="1.0" encoding="utf-8" ?>
<Test>
  <Problem id="1">
    <Question>Which language am I learning right now?</Question>
    <OptionA>VB 7.0</OptionA>
    <OptionB>J2EE</OptionB>
    <OptionC>French</OptionC>
    <OptionD>C#</OptionD>
    <Answer>OptionA</Answer>
  </Problem>
  <Problem id="2">
    <Question>What does XML stand for?</Question>
    <OptionA>eXtremely Muddy Language</OptionA>
    <OptionB>Xylophone, thy Music Lovely</OptionB>
    <OptionC>eXtensible Markup Language</OptionC>
    <OptionD>eXtra Murky Lungs</OptionD>
    <Answer>OptionC</Answer>
  </Problem>
</Test>
Run Code Online (Sandbox Code Playgroud)

就将XML加载到内存而言,.NET提供了许多处理XML文件和字符串的内在方法,其中许多方法完全混淆了必须直接与FileStream交互.例如,该XmlDocument.Load(myFileName.xml)方法将在一行代码内部为您完成.就个人而言,虽然我更喜欢使用XmlReaderXPathNavigator.

有关更多信息,请查看System.Xml命名空间的成员.


Nol*_*rin 5

确实没有固定的方法可以做到这一点,尽管我同意对于简单的测验问题数据库,文本文件可能是您的最佳选择(与 XML 或适当的数据库相反,尽管前者不会完全矫枉过正) .

这是一组测验问题的基于文本格式的小示例,以及将问题读入代码的方法。编辑:我现在试图让它尽可能容易理解(使用简单的结构),并附有大量评论!

文件格式

示例文件内容。

第一个问题的问题文本...
答案 1
答案 2
!Answer 3 (正确答案)
答案 4

第二个问题的问题文本...
!Answer 1 (正确答案)
答案 2
答案 3
答案 4

代码

这只是用于在代码中存储每个问题的简单结构:

struct Question
{
    public string QuestionText; // Actual question text.
    public string[] Choices;    // Array of answers from which user can choose.
    public int Answer;          // Index of correct answer within Choices.
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下代码从文件中读取问题。除了对象初始值设定项(基本上这只是允许您在创建对象的同时设置对象的变量/属性)之外,这里没有什么特别之处。

// Create new list to store all questions.
var questions = new List<Question>();

// Open file containing quiz questions using StreamReader, which allows you to read text from files easily.
using (var quizFileReader = new System.IO.StreamReader("questions.txt"))
{
    string line;
    Question question;

    // Loop through the lines of the file until there are no more (the ReadLine function return null at this point).
    // Note that the ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices.
    while ((line = quizFileReader.ReadLine()) != null)
    {
        // Skip this loop if the line is empty.
        if (line.Length == 0)
            continue;

        // Create a new question object.
        // The "object initializer" construct is used here by including { } after the constructor to set variables.
        question = new Question()
        {
            // Set the question text to the line just read.
            QuestionText = line,
            // Set the choices to an array containing the next 4 lines read from the file.
            Choices = new string[]
            { 
                quizFileReader.ReadLine(), 
                quizFileReader.ReadLine(),
                quizFileReader.ReadLine(),
                quizFileReader.ReadLine()
            }
        };

        // Initially set the correct answer to -1, which means that no choice marked as correct has yet been found.
        question.Answer = -1;

        // Check each choice to see if it begins with the '!' char (marked as correct).
        for(int i = 0; i < 4; i++)
        {
            if (question.Choices[i].StartsWith("!"))
            {
                // Current choice is marked as correct. Therefore remove the '!' from the start of the text and store the index of this choice as the correct answer.
                question.Choices[i] = question.Choices[i].Substring(1);
                question.Answer = i;
                break; // Stop looking through the choices.
            }
        }

        // Check if none of the choices was marked as correct. If this is the case, we throw an exception and then stop processing.
        // Note: this is only basic error handling (not very robust) which you may want to later improve.
        if (question.Answer == -1)
        {
            throw new InvalidOperationException(
                "No correct answer was specified for the following question.\r\n\r\n" + question.QuestionText);
        }

        // Finally, add the question to the complete list of questions.
        questions.Add(question);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,这段代码相当快速和基本(当然需要一些更好的错误处理),但它至少应该说明您可能想要使用的简单方法。我确实认为文本文件是实现这样一个简单系统的好方法,因为它们具有人类可读性(在这种情况下,XML 有点过于冗长,IMO),而且它们与 XML 一样容易解析文件。希望这能让你开始……