如何根据标准更改标签的值

时间:2013-03-14 23:42:52

标签: c# winforms

所以我有一个名为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

感谢您提供任何帮助或建议。

2 个答案:

答案 0 :(得分:1)

difficultySelectionControl_DifficultySelected中,将所选难度存储在类变量m_difficulty中 然后,只需在questionControl_QuestionAnswered

中访问它

在您的班级定义中,添加private Difficulty m_difficultydifficultySelectionControl_DifficultySelected中添加一行m_difficulty = e.Difficulty 那么,你可以在questionControl_QuestionAnswered中使用这个难度,就像@Michael Perrenoud建议的那样。

答案 1 :(得分:1)

这样做:

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

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

你需要明显地从某个地方获取difficulty,而且我现在无法确切地知道它在哪里,但你拥有它,因为你将它传递给方法LoadQuestions和{{1只需抓住它,运行代码,然后BAM就可以获得修改器。

注意:默认情况下,我将修改器设置为GetFileNameFor,我很确定您不希望将其设置为1,因为这会净0每次都是1}}。