设计一个简单的控制台琐事应用程序

时间:2013-11-25 15:59:20

标签: c# random user-input

我们的任务是将C#trivia应用程序设计为文件I / O练习。到目前为止,我有一个良好的开端,但演示步骤有点让我感到困惑。

我开始使用包含以下数据的分隔文件:

Question;CorrectAnswerA;AnswerB;AnswerC;AnswerD;AnswerExplanation

如,

 What color is the sky?;Blue;White;Green;Yellow;The sky is blue.

游戏将显示问题和用户可以选择的四个答案。

What Color is the Sky?
A. Blue
B. White
C. Green
D. Yellow

Select A, B, C, or D: 

不幸的是,为了便于填充数据集,A始终是正确的答案。我想随机化四个答案显示的顺序,但程序仍然需要知道哪个是正确的答案。我还需要将ABCD的用户输入绑定到答案的特定实例,以比较selectedAnswerStringcorrectAnswerString

我一直在玩一个随机填充的四个答案的数组,但是我无法根据用户的选择来判断如何标记正确的内容;我执行或分配的逻辑似乎总是超出范围或重复数组中的所有四个记录。

我接触过的其他学生说他们创建了他们的数据集,其中的答案是预先加扰的(因此他们可以按照读取的顺序打印出来),并使用第五个答案字段来表示正确的答案。虽然这绝对是实现它的简单方法,但我认为它不如我的策略那么优雅。

我应该只更改输入数据集吗?有没有人有任何关于追求我的随机化想法的好方法?

5 个答案:

答案 0 :(得分:1)

创建一个名为Question的类,其中包含以下属性:int Idstring QuestionTextList<string> Answersstring CorrectAnswer

或者,作为前进,还可以使用AnswerId创建课程Value并引用它。

   public class Question
    {
        public int Id;
        public string QuestionText;
        public List<Answer> Answers;
        public Answer CorrectAnswer;
    }

    public class Answer
    {
        public int Id;
        public string Value;
    }

然后填充此结构并在打印时随机化

答案 1 :(得分:0)

使用单独的变量跟踪正确的答案。然后使用Fisher-Yates来重新排列数组:

Random random = new Random();

void Shuffle(string[] answers) {
   for (int i = 0; i < answers.Length - 1; i++) {
      int j = random.Next(i, answers.Length);
      string temp = answers[j];
      answers[j] = answers[i];
      answers[i] = temp;
   }
}

用户回复后,只需将他们的选择与您保存的正确答案进行比较。

答案 2 :(得分:0)

尝试以这种方式为问题定义数据结构:

public class Question
{
    public string Question;
    public string[] Answers;
    public int CorrectAnswer;
    public string CorrectAnswerExplanation
}

这样你就可以加扰数组Answers中的字符串,同时仍然跟踪CorrectAnswer中正确答案的索引。

或者如果你不能使用一个单独的类来模拟问题(这是一个功课问题,所以你可能还没有学过这个问题),你可以使用阵列中的预定位置(第1或第5个) element)保存正确答案的索引。所以你的答案数组看起来像:

"Blue", "White", "Green", "Yellow", "0"

答案 3 :(得分:0)

第1步:定义数据结构。其他人已经给你一个结构,所以使用它。

第2步:填充数据结构。您可以使用System.IO.File.ReadLines并解析每一行 - 我假设您已经处理了这一点。

第3步:随机化答案的顺序。为此,假设您有自己的结构:

public static void RandomiseAnswers(IEnumerable<Question> questions)
{
    var rand = new Random((int)DateTime.Now.Ticks);
    foreach (var question in questions) 
    {
        question.Answers = question.Answers.OrderBy(x => rand.Next()).ToArray();
    }
}

第4步:从老师那里收到金星,以表彰你的出色工作

答案 4 :(得分:0)

我个人在Answer类中将一个布尔值默认为false。这样,当选择答案时,您可以阅读它是否正确。

public class AskQuestion
{
    public int Id;
    public string Question;
    public string Explanation;
    public List<Answer> Answers = new List<Answer>();
}

public class Answer
{
    public bool Correct = false;
    public string Value;
}

现在,当您随机化列表时,会自动识别正确的答案

使用这些类的一种方法是这样的:

    static void Main(string[] args)
    {
        StreamReader sr = new StreamReader("text.txt");
        List<AskQuestion> Questions = new List<AskQuestion>();
        Random rnd = new Random(DateTime.Now.Millisecond);
        //Loop through the file building a list of questions
        while(!sr.EndOfStream)
        {
            AskQuestion NewQuestion = new AskQuestion();
            string[] input = sr.ReadLine().Split(';');
            NewQuestion.Question = input[0];
            NewQuestion.Explanation = input[5];
            for(int i = 1; i < 5; i++)
            {
                Answer NewAnswer = new Answer();
                NewAnswer.Value = input[i];
                NewQuestion.Answers.Add(NewAnswer);
            }
            //The first question is always correct so set its boolean value to true;
            NewQuestion.Answers[0].Correct = true;
            //Now ranmdomize the order of the answers
            NewQuestion.Answers = NewQuestion.Answers.OrderBy(x => rnd.Next()).ToList();
            Questions.Add(NewQuestion);
        }
        //Generate menu and get response for each question
        foreach(AskQuestion q in Questions)
        {
            Console.WriteLine(q.Question + ":\n\tA - " + q.Answers[0].Value + "\n\tB - " + q.Answers[1].Value + "\n\tC - " + q.Answers[2].Value + "\n\tD - " + q.Answers[3].Value + "\n");
            char input = '0';
            while(input < 'A' || input > 'D')
            {
                input = char.ToUpper(Console.ReadKey().KeyChar);
                if(input >= 'A' && input <= 'D')
                {
                    //Use the boolean value in the answer to test for correctness.
                    if(q.Answers[input - 'A'].Correct)
                    {
                        Console.WriteLine("\nCorrect\n");
                    }
                    else
                        Console.WriteLine("\nWrong\n");

                    Console.WriteLine(q.Explanation);
                }
            }
        }
        Console.ReadLine();
    }
相关问题