2013-03-14 3 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

difficultySelectionControl_DifficultySelected에 선택한 난이도를 클래스 변수 m_difficulty에 저장하십시오.
클래스 정의에서 questionControl_QuestionAnswered

에 액세스하고 private Difficulty m_difficulty을 추가하십시오.
difficultySelectionControl_DifficultySelectedm_difficulty = e.Difficulty이라는 줄을 추가하십시오.
그런 다음 @Michael Perrenoud가 제안한 것처럼 questionControl_QuestionAnswered에서 그 어려움을 사용할 수 있습니다.

+0

예제를 제공해 주시겠습니까? 당신이 의미하는 것이 확실하지 않습니다 (용서하세요) – user2141272

+0

예제를 추가했습니다 –

+0

어떤 클래스 정의에'private difficulty m_difficulty'를 추가합니까? – user2141272

1

그냥 이렇게 :

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

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

당신이 어딘가에 분명히에서 difficulty을 얻을해야합니다, 당신은 방법 LoadQuestions로 통과하기 때문에 그것을 내가 정확히 어디에 지금 말할 수는 없지만, 당신은 그리고 GetFileNameFor, 그러니 잡아 코드를 실행하고 BAM에 수정자를 추가하십시오.

참고

: 나는 기본적으로 1에 수정을 설정, 나는 그 결과로 모든 시간을 0 그물 것이기 때문에 당신이 0으로 설정하고 싶지 않았다 확신 해요.

관련 문제