2013-03-14 93 views
1

因此,我有一個名爲lblScore.TextlblScore.Text = iCorrectACount.ToString();的標籤,其中iCorrectACount基本上是一個用戶回答多少個問題的計數器。現在我想要做的就是讓這個數字根據選擇的難度乘以最終分數,即如果選擇簡單的問題,將iCorrectACount乘以0並將其轉換爲字符串,如果選擇中等問題,則將iCorrectACount乘以1.5,轉換爲字符串,如果選擇了難題,請將iCorrectACount乘以2並轉換爲字符串,但我不確定該如何做。如何根據標準更改標籤的價值

我的代碼是這樣的:

private void QuizReset() 
{ 
    // Resets the difficulty selection control and shows it again upon resetting the quiz 
    difficultySelectionControl.Reset(); 
    difficultySelectionControl.BringToFront(); 

    // Disabled the 'Next' button and prompts the user to select a difficulty - User cannot continue without choosing 
    btnNext.Enabled = false; 
    lblStatus.Text = "Please select a difficulty"; 

    // Sets the number of questions and correct answers to zero 
    iCorrectACount = 0; 
    iCurrentQIndex = 0; 
} 

private void LoadQuestions(Difficulty difficulty) 
{ 
    // Defines a random variable to be used to shuffle the order of the questions and answers 
    var rand = new Random(); 
    // Loads the corresponding XML document with 'Easy', 'Medium' or 'Hard' questions depending on difficulty chosen 
    var xdoc = XDocument.Load(GetFileNameFor(difficulty)); 

    // List of questions that are filtered from the XML file based on them being wrapped in question tags 
    _questions = xdoc.Descendants("question") 
     .Select(q => new Question() 
     { 
      ID = (int)q.Attribute("id"), 
      Difficulty = (int)q.Attribute("difficulty"), 
      QuestionText = (string)q.Element("text"), 
      Answers = q.Element("answers") 
       .Descendants() 
       // Stores all answers into a string 
       .Select(a => (string)a) 
       // Randomizing answers 
       .OrderBy(a => rand.Next()) 
       .ToArray(), 
      CorrectAnswer = (string)q.Element("answers") 
       .Descendants("correctAnswer") 
       // Use value instead of index 
       .First() 
     }) 
     // Selects questions that match the difficulty integer of the option the user chose 
     .Where(q => q.Difficulty == (int)difficulty + 1) 
     // Randomizing questions 
     .OrderBy(q => rand.Next()) 
     .ToList(); 

    lblStatus.Text = String.Format("There are {0} questions in this section", _questions.Count); 
} 

private string GetFileNameFor(Difficulty difficulty) 
{ 
    switch (difficulty) 
    { 
     case Difficulty.Easy: return "quiz_easy.xml"; 
     case Difficulty.Medium: return "quiz_medium.xml"; 
     case Difficulty.Hard: return "quiz_hard.xml"; 
     default: 
      throw new ArgumentException(); 
    } 
}  

private void PickQuestion() 
{ 
    questionControl.DisplayQuestion(_questions[iCurrentQIndex]); 
    questionControl.BringToFront(); 
    iCurrentQIndex++; 
} 

private void FormMain_Load(object sender, EventArgs e) 
{ 
    QuizReset(); 
    lblScore.Text = "0"; 
} 

private void miNewQuiz_Click(object sender, EventArgs e) 
{   
    QuizReset(); 
    lblScore.Text = "0"; 
} 

private void miExit_Click(object sender, EventArgs e) 
{ 
    Close(); 
} 

private void miHelp_Click(object sender, EventArgs e) 
{ 
    FormHowToPlay form = new FormHowToPlay(); 
    form.ShowDialog(); 
} 

private void miAbout_Click(object sender, EventArgs e) 
{ 
    AboutBox1 aboutForm = new AboutBox1(); 
    aboutForm.ShowDialog(); 
} 

private void btnNext_Click(object sender, EventArgs e) 
{ 
    if (iCurrentQIndex < _questions.Count) 
    { 
     PickQuestion(); 
     lblStatus.Text = String.Format("Question {0} of {1}", iCurrentQIndex, _questions.Count); 
    } 
    else 
    { 
     btnNext.Enabled = false; 
     lblStatus.Text = String.Format("You answered {0} questions correctly out of a possible {1}", 
             iCorrectACount, _questions.Count); 

     this.Hide(); 

     SummaryForm sumForm = new SummaryForm(); 
     DialogResult result = sumForm.ShowDialog(); 

     MenuForm mnuform = new MenuForm(); 
     mnuform.ShowDialog(); 
    } 
} 

    private void difficultySelectionControl_DifficultySelected(object sender, DifficultySelectedEventArgs e) 
    { 
     iCurrentQIndex = 0; 
     LoadQuestions(e.Difficulty);   

     btnNext.Enabled = true; 
    } 


    private void questionControl_QuestionAnswered(object sender, QuestionAnsweredEventArgs e) 
    { 
     if (e.IsCorrect) 
      iCorrectACount++; 

     lblScore.Text = iCorrectACount.ToString(); 

    } 

這是我需要找出最後的小東西,我無法弄清楚如何得到它,這樣,如果難度=易/中/硬,乘以iCorrectAmount乘以1/1.5/2/0。

感謝您的任何幫助或建議。

回答

1

in difficultySelectionControl_DifficultySelected,將選定的難度存儲在類變量m_difficulty中。
然後,只需訪問它在questionControl_QuestionAnswered

在您的類定義中,添加private Difficulty m_difficulty
in difficultySelectionControl_DifficultySelected,添加一行說m_difficulty = e.Difficulty
然後,你可以在你的questionControl_QuestionAnswered中使用這個難度,就像@Michael Perrenoud建議的那樣。

+0

你能舉個例子嗎?不太確定你的意思(原諒我) – user2141272 2013-03-14 23:53:54

+0

我已經添加了一個示例 – 2013-03-15 09:32:04

+0

在哪個類定義中添加'private Difficulty m_difficulty' – user2141272 2013-03-15 11:34:43

1

只是這樣做:

int modifier = 1; 
if (difficulty == Difficulty.Medium) { modifier = 1.5; } 
if (difficulty == Difficulty.Hard) { modifier = 2; } 

lblScore.Text = (iCorrectACount * modifier).ToString(); 

你需要從什麼地方獲得明顯的difficulty,我不能告訴確切位置的權利,但你擁有了它,因爲你通過它進入方法LoadQuestionsGetFileNameFor,所以只需抓住它,運行代碼,然後BAM獲得修改器。

注:我的修改設置爲1默認情況下,我敢肯定你不想將它設置爲0因爲這將每一次淨0作爲結果。