2013-03-14 88 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作为结果。