试图找出我的项目运行不正常的原因

时间:2014-04-27 20:39:22

标签: c# arrays file

我的程序从用逗号分隔的文本文件中读取。答案是一系列字符串,我无法在控制台中填充。

问题将显示,正确的答案(单个字符)将显示并且解释将显示,它只是具有我遇到问题的答案的数组。

我将提供以下代码。

问题单元(文本文件中的问题结构)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoodTalkTrivia
{
    class QuestionUnit
    {
        private string correctAnswer;
        private string explanation;
        private string question;
        private string[] answer = new string[4];

        public string CorrectAnswer
        {
            get { return correctAnswer; }
            set { correctAnswer = value; }
        }
        public string Explanation
        {
            get { return explanation; }
            set { explanation = value; }
        }
        public string Question
        {
            get { return question; }
            set { question = value; }
        }
        public string[] Answer
        {
            get {return answer; }
            set { answer = value; }
        }
    }
}

问题库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace HoodTalkTrivia
{
    class QuestionBank
    {
        List<QuestionUnit> theQuestionsList = new List<QuestionUnit>();

       // private string[] Questions;
       const int NUM_ANSWERS = 4;
       const int NUM_QUESTIONS = 5;

       public int GetNumberOfAnswers
       {
           get { return NUM_ANSWERS; }
       }
       public int GetNumberOfQuestions
       {
           get { return NUM_QUESTIONS; }
       }
       public string GetCorrectAnswer(int index)
       {
           return theQuestionsList[index].CorrectAnswer;
       }
       public string GetExplanation(int index)
       {
           return theQuestionsList[index].Explanation;
       }
       public string GetQuestion(int index)
       {
           return theQuestionsList[index].Question;
       }
       public string[] GetAnswer(int index)
       {
           return theQuestionsList[index].Answer;
       }
       public bool ReadQuestionFile()
       {
           bool success = true;

           FileInfo httSourceFile = new FileInfo("Questions.txt");
           string line;
           string[] fields;
           char[] delimiter = { System.Convert.ToChar(",") };
           QuestionUnit httQuestionUnit;
           //int i = 0;

           try
           {
               StreamReader httReader = httSourceFile.OpenText();

               line = httReader.ReadLine();

               while (line != null)
               {
                   httQuestionUnit = new QuestionUnit();

                   fields = line.Split(delimiter);

                   httQuestionUnit.Question = fields[0];

                   string[] aArray =  new string[4];

                   aArray[0] = fields[1];                    
                   aArray[1] = fields[2];
                   aArray[2] = fields[3];
                   aArray[3] = fields[4];

                   httQuestionUnit.Answer = aArray;
                   httQuestionUnit.CorrectAnswer = fields[5];
                   httQuestionUnit.Explanation = fields[6];


                   theQuestionsList.Add(httQuestionUnit);

                   line = httReader.ReadLine();
               }

           }
           catch
           {
               success = false;
           }
           return success;
       }

   }
}

主要

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HoodTalkTrivia
{
    class HoodTalkTriviaGame
    {
        HoodTalkTriviaStrings myGameStrings;


        public void PlayAgain()
        {
        }

        public void Play()
        {
            QuestionBank myQuestions = new QuestionBank();
            DisplayWelcome();

            myQuestions.ReadQuestionFile();

            for (int i = 0; i < myQuestions.GetNumberOfQuestions; i++)
            {
                Console.WriteLine(myQuestions.GetQuestion(i));

                for (int j = 0; j < myQuestions.GetNumberOfAnswers; j++)
                {
                    Console.WriteLine(myQuestions.GetAnswer(j));
                }
                // Console.WriteLine(myQuestions.GetCorrectAnswer(i));
                Console.WriteLine("Make a selection, A - D");
                Console.ReadLine();
            }




        }

        public void DisplayWelcome()
        {
            myGameStrings = new HoodTalkTriviaStrings();

            myGameStrings.WelcomeString();
        }

        public char PromptForGuess()
        {
            char guess = ' ';


            return guess;
        }
    }
}

以下是我在控制台上获得的图片。apprunning

0 个答案:

没有答案