2017-02-11 64 views
-1

我正在做一个测验型游戏,我正在试图决定处理它的最佳结构。嵌套列表或任何其他结构? c#

10个或更多问题,每个问题都会有多个答案,但都是错误的,只有一个答案。

用户将从列表中随机选取的4个可见答案(单选按钮)中选择正确的答案。它会保证正确的答案总是被提起。

列表将具有多个子列表内(每一个的提问和多个答案)

在那些子列表的第一位置[0]将是问题,其余全部[1 ... ]将是答案。

我的问题是,嵌套列表是这样做的正确方法?我在想这个对吗?

在此先感谢!

+0

怎么样了'名单',其中'Question'是你定义为有答案类(*为像'.A','.B','.C'和'。 D' *)如果它总是会有4个答案。否则,如果答案不是一个固定的数额,它可以有一个列表/数组答案。注意SO是用于编写代码有问题或问题的问题/问题,或询问有关特定功能,工作代码和建议,以最好的方式来完成检查[CodeReview](http://codereview.stackexchange.com/ )。 –

回答

1

对于绑定目的等,我建议使用自定义类的列表;如:

var questionList = new List<QandA>(); 

///... 

public class QandA 
{ 
    public string Question { get; set; } 
    public string Answer1 { get; set; } 
    public string Answer2 { get; set; } 
    public string Answer3 { get; set; } 
    public string Answer4 { get; set; } 
    internal int CorrectIndex { get; set; } 
} 
1

我可能会做一个类。

public class Question { 
    public string TheQuestion; 
    public string[] TheAnswers; //first should be correct answer 
    public Question(string quest, string[] ans){ 
     TheQuestion = quest; 
     TheAnswers = ans; 
    } 
} 

然后,你可以制作一个List<Question> Questions你喜欢的所有问题,并从他们拉。在设置答案时,总是让第一个数组的值为正确答案,然后您可以随机化该顺序以显示答案。

List<Question> Questions = new List<Question>(); 
string q = "Is stackoverflow the best?"; 
string[] a = new string {"Yes!", "No.", "Probably Not" }; 
Question q1 = new Question(q, a); 
Questions.Add(q1); 
0

“列表清单”可以工作,但可能非常有限。你不会有很多机会组织你的程序的逻辑。

相反,您应该创建类来表示模型中的“事物”。寻找重要的名词:测验,问题和答案。

class Quiz { 
    List<Question> mQuestions = new List<Question>(); 

    // implement functionality that is relevant at the Quiz level 
} 

class Question { 
    string mQuestionText; 
    List<Answer> mAnswers = new List<Answer>();  

    // implement functionality that is relevant at the Question level 
} 

class Answer { 
    string mAnswerText; 
    bool mIsCorrect; 

    // implement functionality that is relevant at the Answer level 
} 
1

这里有一些很好的答案,但是我想让组织的事情有点不同。而不是回答1总是正确的。你可以像下面这样做,将它展现出来,在随机化多项选择时提供更多的灵活性,并且如果答案是正确的,则可以更容易地进行比较。

// core class 
    public static class Quiz 
    { 
     private static Random rnd = new Random(); 
     public static Question[] Questions = new[] 
     { 
      new Question 
      { 
       QuestionText = "Sample uestion 1?", 
       CorrectAnswer = "Answer to question 1", 
       WrongAnswers = new [] { 
        "Wrong answer 1", 
        "Wrong answer 2", 
        "Wrong answer 3", 
        "Wrong answer 4", 
        "Wrong answer 5", 
       } 
      }, 
      new Question 
      { 
       QuestionText = "Sample uestion 2?", 
       CorrectAnswer = "Answer to question 2", 
       WrongAnswers = new [] { 
        "Wrong answer 1", 
        "Wrong answer 2", 
        "Wrong answer 3", 
        "Wrong answer 4", 
        "Wrong answer 5", 
       } 
      } 
     }; 

     public class Question 
     { 
      public string QuestionText { get; set; } 
      public string CorrectAnswer { get; set; } 
      public string[] WrongAnswers { get; set; } 

      public string[] GetMultipleChoice(int numberOfChoices) 
      { 
       var list = new List<string>() { CorrectAnswer }; 
       list.AddRange(WrongAnswers.Take(numberOfChoices - 1)); 

       // shuffle 
       int n = list.Count; 
       while (n > 1) 
       { 
        n--; 
        int k = rnd.Next(n + 1); 
        var value = list[k]; 
        list[k] = list[n]; 
        list[n] = value; 
       } 

       return list.ToArray(); 
      } 
     } 
    } 

    // console application 
    static void Main(string[] args) 
    { 
     foreach (var question in Quiz.Questions) 
     { 
      // write question 
      Console.WriteLine(question.QuestionText); 
      Console.WriteLine(string.Join(Environment.NewLine, question.GetMultipleChoice(4))); 

      var response = Console.ReadLine(); 

      if (response.Equals(question.CorrectAnswer, StringComparison.InvariantCultureIgnoreCase)) 
      { 
       Console.WriteLine("Correct!"); 
      } 
      else 
      { 
       Console.WriteLine("Wrong"); 
      } 
     } 
    }