0

How can I print out a list of students (from this dictionary) with a score above 40, please: I would like to use an "If", else statement. But, I need help with how to get the computer to pick the 2nd value pair; which is the score & check if it is greater than 40 or not, please.

Code:

Dictionary<string, int> studentResults = new Dictionary<string, int>();
            studentResults.Add("Fred", 85);
            studentResults.Add("Chris", 72);
            studentResults.Add("Paul", 65);
            studentResults.Add("Marcus", 50);
            studentResults.Add("Ashley", 22);

            foreach (var studentResult in studentResults)
            {
                Console.WriteLine("Student " + studentResult.Key + " has a mark of " + studentResult.Value);
            }

            // Print the list of students with scores above 40
            // List of students with scores above "40" - Fred, Chris, Paul & Marcus.

1 Answer 1

0

You are on the correct path, if you would like to print out a list of students that have attained marks greater than 40, then within your loop, just add the condition as follows:

foreach (var studentResult in studentResults)
    {
        if(studentResult.Value > 40)
        {
        Console.WriteLine("Student " + studentResult.Key + " has a mark of " + studentResult.Value);
        }
    }
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.