C#
Score calculation
Learn correct count and percentage with ScoreCalculator.
Score tracking
In this lesson we update the Run method to track the score. The method will return int instead of void; it will increment a counter on each correct answer.
First declare int score = 0; in the body. In the correct branch, increment with score++. At the end, return the result with return score;. In Main, get the result with int score = quiz.Run(answers); and print it with Console.WriteLine(score);.
Return type and return
A method's return type is the type of value it produces. void means no return; int returns an integer. The return score; statement ends the method and gives the value to the caller.
When you change Run to public int Run(string[] answers), the body must contain a return; otherwise you get a compile error. In Main you can capture the returned value: int score = quiz.Run(answers);.
Exercise Compute and print the score
Change Run's return type from void to int. In the body declare int score = 0;, increment with score++ in the correct branch and add return score; at the end. In Main, capture the result with int score = quiz.Run(new[] { "Ankara" }); and print it with Console.WriteLine(score);.
Show example solution
Update Run as `public int Run(string[] answers) { int score = 0; for (int i = 0; i < questions.Count; i++) { Console.WriteLine(questions[i].Text); if (answers[i] == questions[i].Answer) { Console.WriteLine("Correct"); score++; } else { Console.WriteLine("Wrong"); } } return score; }`. In Main write `var quiz = new Quiz(); quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" }); int score = quiz.Run(new[] { "Ankara" }); Console.WriteLine(score);`.
Example solution:
using System;
using System.Collections.Generic;
class Question
{
public string Text;
public string Answer;
}
class Quiz
{
private List<Question> questions = new List<Question>();
public void AddQuestion(Question q)
{
questions.Add(q);
}
public int Count()
{
return questions.Count;
}
public int Run(string[] answers)
{
int score = 0;
for (int i = 0; i < questions.Count; i++)
{
Console.WriteLine(questions[i].Text);
if (answers[i] == questions[i].Answer)
{
Console.WriteLine("Correct");
score++;
}
else
{
Console.WriteLine("Wrong");
}
}
return score;
}
}
class Program
{
static void Main()
{
var quiz = new Quiz();
quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" });
int score = quiz.Run(new[] { "Ankara" });
Console.WriteLine(score);
}
}
Test yourself
-
What is the correct way to compute the score?
- Keep Run void and count in Main.
- Run should return int and increment on each correct answer.
- Add a separate CalculateScore method.
- Keep the score in a global variable.
Show answer
Run should return int and increment on each correct answer. — Correct. If Run returns int, the score is easy to read in Main.
-
Fill in the blank: score increment
if (answers[i] == questions[i].Answer)
{
Console.WriteLine("Correct");
____;
}
Show answer
if (answers[i] == questions[i].Answer)
{
Console.WriteLine("Correct");
score++;
}
-
Match: score parts
- int score
- score counter
- score++
- correct answer increment
- return score
- returns the result
- WriteLine(score)
- prints the score
Next lesson: Putting it together