2013-11-25 60 views
0

我們一直負責設計C#應用瑣事的文件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總是正確的答案。我想隨機其中四個答案的顯示順序,但該程序仍需要知道哪個是正確答案。我還需要將用戶輸入AB,CD與回答的特定實例綁定以將selectedAnswerStringcorrectAnswerString進行比較。

我一直在玩與隨機填充四個答案的數組,但我不能換我周圍的頭怎麼標誌的東西是正確的根據用戶的選擇;我的邏輯做或分配總是似乎超出範圍或在數組中的所有四個記錄重複。

我已經說過了

其他學生說,他們創建了數據集與答案加擾的預(這樣他們可以打印出來的順序讀取)與正確答案第五答案場。雖然絕對是一種簡單的方法來實現它,但我認爲它不如我的策略那麼優雅。

如果我只是改變輸入數據集?有沒有人有任何想法來追求我的隨機化想法?使用一個單獨的變量正確答案的

回答

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; 
    } 
} 

後,只需比較其與選擇您已經保存了正確的答案。

+0

我如何跟蹤用戶的選擇和答案? 'switch'是管理'A'的最好方式是'answers [1]',''B''''''等等。還是有更好的策略? – dwwilson66

+0

@ dwwilson66這很好,但您需要記住數組從零開始索引。你可以使用'answers [selectedChar - 'A']',這會給你'A = 0,B = 1,...'。 – Zong

+0

它早...沒有咖啡。 :) 我知道。 – dwwilson66

1

創建一個名爲Question與屬性的類:int Idstring QuestionTextList<string> Answersstring CorrectAnswer

或者,如向前邁出一步,也創造了AnswerIdValue和引用。

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; 
    } 

然後填充這個結構,隨機打印

0

嘗試定義你的數據結構的問題,在這樣的時候:

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

這樣你可以打亂字符串數組Answers中,同時仍在跟蹤CorrectAnswer中正確答案的索引。或者,如果你不能使用單獨的類來模擬問題(這是一個家庭作業問題,所以你可能還沒有學到這個),你可以使用數組中的預定位置(第1個或第5個元素)保存正確答案的索引。因此,您的答案陣列如下所示:

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

我們已經學會了如何使用單獨的課程......但是我所說的其他所有學生都在他們的數據集中使用了一個字段來保存正確的答案標記。就我個人而言,我認爲這打破了可重用性的目的,並且我試圖避免對可以以編程方式確定的任何東西進行硬編碼。我的導師和我在整個學期都在關於對象或數據結構需要或應該如何通用的問題。但這是人際關係--overflow.com的主題。 ;) – dwwilson66

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步:收到金星從老師爲你出色的工作

+0

我希望。她是爲我們提供的UML添加方法的碼頭。但我喜歡這個......只是爲了惹她。我有一個關於這一行的問題:'var rand = new Random((int)DateTime.Now.Ticks);'它是使用當前時間作爲種子隨機化的嗎?我不熟悉它的'.Ticks'部分,現在就來看看MSDN吧。 :) – dwwilson66

+0

@ dwwilson66:是的,你是正確的 – Ben

0

個人而言,我會把一個布爾值在應答類默認爲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(); 
    } 
+0

那麼這樣的答案不能在不同的問題中重複使用。 – Nogard

+0

每個問題都有自己的答案列表。重新使用不同問題的答案沒有多大意義。該類的每個實例都有自己的字符串和布爾值。 – tinstaafl

+0

「它自己的答案列表」 - 真,但是「唯一答案列表」 - 不是必須的,因爲可能存在「真/假」問題或諸如「2^3 =」或「字節中有多少位」等問題。這不是必然性,而是一種可能性,在這種情況下,重用已經存在的答案比創建新的重複項更明智 – Nogard

相關問題