如何根据条件改变标签的值

本文关键字:标签 改变 条件 何根 | 更新日期: 2023-09-27 18:04:03

我有一个名为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("answerswers")
                .Descendants()
                // Stores all answers into a string
                .Select(a => (string)a)
                // Randomizing answers
                .OrderBy(a => rand.Next()) 
                .ToArray(),
            CorrectAnswer = (string)q.Element("answerswers")
                .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。

谢谢你的帮助和建议。

如何根据条件改变标签的值

就这样做:

int modifier = 1;
if (difficulty == Difficulty.Medium) { modifier = 1.5; }
if (difficulty == Difficulty.Hard) { modifier = 2; }
lblScore.Text = (iCorrectACount * modifier).ToString();

你显然需要从某个地方获得difficulty,我现在不能确切地说在哪里,但你有它,因为你把它传递给方法LoadQuestionsGetFileNameFor,所以只要抓住它,运行代码,然后你就得到了你的修改器。

注意:我将修改符设置为1默认情况下,我很确定你不想将其设置为0,因为这会使0作为每次的结果

difficultySelectionControl_DifficultySelected中,将选择的难度存储在类别变量m_difficulty中。
然后,在questionControl_QuestionAnswered

中访问它

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