流阅读器不会移动到文件中的下一条记录

时间:2014-09-11 16:59:04

标签: c# indexing position streamreader

我正在开发一个包含50个问题的测验,测验需要5个问题级别,用户根据正确和错误的答案上下移动。对于我的1级问题,我已经设置了一个包含10种问题的文本文件:

http://imgur.com/Or60Yut

我为问题@ level 1创建了一个问题结构和一个数组:

class Program
{
    static Question[] _Questions1 = new Question[10];

    static int score = 0;
    static int asked = 0;
    static int user_input = 0;
    static int user_level = 1;
    static int pos = 1;

    struct Question
    {
        public string q_no;
        public string question;
        public string choices;
        public string answer;
    }    

我已经编写了一个流阅读器来使用我的数组读取我的文本文件:

static void Level_1()
    {
        Console.Clear();
        Question _Questions1 = new Question();
        string filename = @"Files\question1.txt";

        using (StreamReader sreader = new StreamReader(filename, true))
        {
            asked += 1;
            pos = 1;
            _Questions1.q_no = sreader.ReadLine();
            Console.WriteLine(_Questions1.q_no);
            _Questions1.question = sreader.ReadLine();
            Console.WriteLine(_Questions1.question);
            _Questions1.choices = sreader.ReadLine();
            Console.WriteLine(_Questions1.choices);
            _Questions1.answer = sreader.ReadLine();
            user_input = Convert.ToInt32(Console.ReadLine());

            if (user_input == Convert.ToInt32(_Questions1.answer))
            {
                score += 2;
                user_level += 1;
                Console.WriteLine("\nCongratulations you have answered correctly, scored 2 points and advanced to level 2!");
                Console.WriteLine("Score = {0}, Questions Asked = {1}", score, asked);
            }
            if (user_input != Convert.ToInt32(_Questions1.answer))
            {
                Console.WriteLine("\nUnfortunately you are incorrect and remain at level 1, please try again!");
                pos++;
                asked++;
                Level_1();
            }
        }

这适用于阅读第一个问题,但每当我尝试使用_Questions1[pos].q_no应用索引以便一旦用户回答错误就转移到下一个问题,我会收到构建错误说明:

Error 1 Cannot apply indexing with [] to an expression of type 'Quiz_Application_Rewritten.Program.Question

我意识到我已经在另一个帐户上提出了这个确切的问题,我已经忘记了它的详细信息,因此我创建了这个,因为我仍然卡住了,我无法进入我的课程的第2年而没有这个应用程序已经完成,并且最近激发了我自己进入成为程序员的心态,我将非常感谢您提供的任何帮助。

我尝试尽可能具体,但如果它仍然没有任何用途,请道歉

我之前尝试过的循环:

static void Questions1(Question _Questions1)
    {
        string[] q_no;
        string[] question;
        string[] choices;
        string[] answer;

        q_no = new string[10];
        question = new string[10];
        choices = new string[10];
        answer = new string[10];
        string filename = @"Files\question1.txt";

        using (StreamReader sreader = new StreamReader(filename, true))
        {
            for (int x = 0; x < 10; x++)
            {
                q_no[x] = _Questions1[pos].q_no = sreader.ReadLine();
            }
        }

q_no[x] = _Questions1[pos].q_no = sreader.ReadLine();抛出无法应用索引错误

1 个答案:

答案 0 :(得分:0)

也许你正在寻找这样的东西?

    static void Level_1()
    {
        Console.Clear();
        _Questions1 = new List<Question>();
        string filename = @"Files\question1.txt";
        Question question = new Question();

        using (FileStream f = File.Open(filename, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(f))
            {
                string read = sr.ReadLine();
                int qNum = 1;

                while (read != null)
                {
                    switch (qNum)
                    {
                        case 1:
                            question.q_no = read;
                            break;
                        case 2:
                            question.question = read;
                            break;
                        case 3:
                            question.choices = read;
                            break;
                        case 4:
                            question.answer = read;
                            _Questions1.Add(question);
                            question = new Question();
                            qNum = -1;
                            break;
                    }
                    qNum++;
                }
            }
            int qqNum = 0;
            do
            {
                question = _Questions1[qqNum];
                Console.WriteLine(question.q_no);
                Console.WriteLine(question.question);
                Console.WriteLine(question.choices);

                user_input = Convert.ToInt32(Console.ReadLine());

                if (user_input == Convert.ToInt32(question.answer))
                {
                    score += 2;
                    qqNum++;
                    Console.WriteLine("\nCongratulations you have answered correctly, scored 2 points and advanced to level 2!");
                    Console.WriteLine("Score = {0}, Questions Asked = {1}", score, asked);
                }
                if (user_input != Convert.ToInt32(question.answer))
                {
                    Console.WriteLine("\nUnfortunately you are incorrect and remain at level 1, please try again!");
                    pos++;
                    asked++;
                    Level_1();
                }
            } while (qqNum < _Questions1.Count);

        }
    }