How To Compare Values In A Key Pair In A Dictionary & Confirm If It’s Greater Than 40

Updated on February 26, 2019 in General
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on February 26, 2019

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.

Many Thanks.

Regards.

  • Liked by
Reply
0 on February 26, 2019

This Is The Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dictionaries
{
    class Program
    {
        static void Main(string[] args)
        {
                   
            Dictionary<string, int> studentResults = new Dictionary<string, int>();
            studentResults.Add(“Fred”, 80);
            studentResults.Add(“Phil”, 75);
            studentResults.Add(“Ashley”, 70);
            studentResults.Add(“Jesse”, 65);
            studentResults.Add(“Boston, 22);
           

            // Assignment No. 2) Print the list of students with scores above 40
           
            // List of students with scores above “40” – Fred, Phil, Ashley& Jesse.

            foreach (var studentResult in studentResults)

            if (studentResult.Value > 40)

            {
                Console.WriteLine(“Student ” + studentResult.Key + ” has a score of ” + studentResult.Value);
            }

        }
    }
}

 
 

Thank you.

  • Liked by
Reply
Cancel