im new to c# programing and i was doing an console application for a friend who was creating a 3 question test. i need to get the name of the top 5 users and display their grade, but i don’t know what to do. could you please help me thanks. This are the codes:
string Name, yn;
int points = 0;
do{
Console.WriteLine("Please enter your fullname here:");
Name = Console.ReadLine();
Console.WriteLine(" ");
Console.WriteLine("Hello " + Name + " Welcome to this simple test.");
Console.WriteLine(" ");
Console.WriteLine("1) What is 5 + 6?");
Console.WriteLine(" a)10");
Console.WriteLine(" b)30");
Console.WriteLine(" c)11");
Console.Write("Answer: ");
string QAns1 = "C";
string MyAns1 = Console.ReadLine().ToUpper();
Console.Clear();
if (MyAns1 == QAns1)
{
Point++;
}
Console.WriteLine("2) What is the first letter of Apple?");
Console.WriteLine(" a)A");
Console.WriteLine(" b)c");
Console.WriteLine(" c)a");
Console.Write("Answer: ");
string QAns2 = "A";
string MyAns2 = Console.ReadLine().ToUpper();
Console.Clear();
if (MyAns2 == QAns2)
{
Point++;
}
Console.WriteLine("3) What is the plural word of tooth?");
Console.WriteLine(" a)tentacles");
Console.WriteLine(" b)Teeth");
Console.WriteLine(" c)tooths");
Console.Write("Answer: ");
string QAns3 = "B";
string MyAns3 = Console.ReadLine().ToUpper();
Console.Clear();
if (MyAns3 == QAns3)
{
Point++;
}
Console.WriteLine(" Mr. " + Name + " your final score is " + Point + "/10 ");
Console.WriteLine(" Do you want to try again? ");
yn = Console.ReadLine().ToUpper();
}while (yn== "Y");
Console.WriteLine("Thank you for using our program.");
There are tons of ways to do this, but to get you started, I’ve added some parts to your code that you can play with.
After each game is finished, you can add the score and name to a collection.
The collctions of scores with the persons name as a key:
var playedGames = new Dictionary<string, int>();Then when each game is finished, you can add the score to the collection like this:
playedGames.Add(Name, Point);Then, when no more games are going to be played, you can order the collection by the top scorers and take out 5 of these like this:
var topScorers = playedGames.OrderByDescending(x => x.Value).Take(5);Then you can print out these 5 top players:
Here’s a complete sample on how you can do it: