我正在做一個測驗型遊戲,我正在試圖決定處理它的最佳結構。嵌套列表或任何其他結構? c#
10個或更多問題,每個問題都會有多個答案,但都是錯誤的,只有一個答案。
用戶將從列表中隨機選取的4個可見答案(單選按鈕)中選擇正確的答案。它會保證正確的答案總是被提起。
列表將具有多個子列表內(每一個的提問和多個答案)
在那些子列表的第一位置[0]將是問題,其餘全部[1 ... ]將是答案。
我的問題是,嵌套列表是這樣做的正確方法?我在想這個對嗎?
在此先感謝!
我正在做一個測驗型遊戲,我正在試圖決定處理它的最佳結構。嵌套列表或任何其他結構? c#
10個或更多問題,每個問題都會有多個答案,但都是錯誤的,只有一個答案。
用戶將從列表中隨機選取的4個可見答案(單選按鈕)中選擇正確的答案。它會保證正確的答案總是被提起。
列表將具有多個子列表內(每一個的提問和多個答案)
在那些子列表的第一位置[0]將是問題,其餘全部[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; }
}
我可能會做一個類。
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);
「列表清單」可以工作,但可能非常有限。你不會有很多機會組織你的程序的邏輯。
相反,您應該創建類來表示模型中的「事物」。尋找重要的名詞:測驗,問題和答案。
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總是正確的。你可以像下面這樣做,將它展現出來,在隨機化多項選擇時提供更多的靈活性,並且如果答案是正確的,則可以更容易地進行比較。
// 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");
}
}
}
怎麼樣了'名單',其中'Question'是你定義爲有答案類(*爲像'.A','.B','.C'和'。 D' *)如果它總是會有4個答案。否則,如果答案不是一個固定的數額,它可以有一個列表/數組答案。注意SO是用於編寫代碼有問題或問題的問題/問題,或詢問有關特定功能,工作代碼和建議,以最好的方式來完成檢查[CodeReview](http://codereview.stackexchange.com/ )。 –