1

所以我有一个标签叫做lblScore.TextwherelblScore.Text = iCorrectACount.ToString();基本上iCorrectACount是一个用户回答正确的问题的计数器。现在我想要做的基本上是使这个数字根据选择的难度乘以最终分数,即如果选择简单的问题,乘以iCorrectACount0 并转换为字符串,如果选择中等问题,乘以iCorrectACount1.5 并转换为字符串,如果选择了难题,乘以iCorrectACount2 并转换为字符串,但我不确定我会怎么做。

我的代码是这样的:

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();

    }

这是我需要弄清楚的最后一件小事,我不知道如何得到它,所以如果难度 = 容易/中等/困难,则乘以iCorrectAmount1/1.5/2/0。

感谢您的任何帮助或建议。

4

2 回答 2

1

在 中difficultySelectionControl_DifficultySelected,将选定的难度存储在类变量中,m_difficulty
然后,只需访问它questionControl_QuestionAnswered

在您的类定义中,添加private Difficulty m_difficulty.
difficultySelectionControl_DifficultySelected,添加一行说m_difficulty = e.Difficulty
然后,您可以questionControl_QuestionAnswered像@Michael Perrenoud 建议的那样在您的 中使用该困难。

于 2013-03-14T23:45:46.380 回答
1

只需这样做:

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

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

difficulty显然需要从某个地方获取,我现在无法确切地知道在哪里,但是您拥有它,因为您将它传递给了方法LoadQuestionsand GetFileNameFor,所以只需抓住它,运行代码,然后 BAM 你就得到了你的修饰符.

注意:我将修饰符1默认设置为,我很确定您不想将其设置为,0因为0每次都会产生结果。

于 2013-03-14T23:47:19.443 回答