-2

I am supposed to be making a dictionary of lists in order to show the scores of different peoples test, the final outcome is supposed to look like this: enter image description here

Currently my code looks like this, the issue I am having is that the numbers are printing 3 times, not once like shown. HELP PLEASE!

static void Main(string[] args)
{
    Random myRandomGenerator = new Random();

    Dictionary<string, List<int>> table = new Dictionary<string, List<int>>();

    table["Meuleveld, McKenzie"] = new List<int>(){ myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60,100), myRandomGenerator.Next(60,100)};
    table["McGuire, Matthew"] = new List<int>(){ myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100) };
    table["Anderton, Paitlyn"] = new List<int>(){ myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100) };
    table["Moore, Jeni"] = new List<int>(){ myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100), myRandomGenerator.Next(60, 100) };

    foreach (string name in table.Keys)
    {
        List<int> value = table[name];

        foreach (int valueList in value)
        {

                Console.WriteLine($"{name} exam scores: {valueList}, {valueList}, {valueList}");

            Console.ReadKey();
        }
    }
}
soccer7
  • 3,547
  • 3
  • 29
  • 50
newcoder
  • 5
  • 1

2 Answers2

1

try this, I did first I use Console.Write rather than WriteLine so the grade of the score will show to the next of the name. then Add Console.WriteLine after foreach for next person will print it to next line.

using Math.Round(average,2) meaning it will round up your average variable to nearest hundredths.

foreach (string name in table.Keys)
{
    List<int> value = table[name];
    double totalGrade = 0;
    Console.Write($"{name} exam scores: ");
    foreach (int valueList in value)
    {

        Console.Write($" {valueList}");
        double grade = valueList;
        totalGrade = grade + totalGrade;
    }
    double avarage = totalGrade / value.Count();
    Console.WriteLine($"");
    Console.WriteLine($"Average: {Math.Round(avarage,2)}");
    Console.WriteLine($"");
    Console.ReadKey();
}

enter image description here

soccer7
  • 3,547
  • 3
  • 29
  • 50
justinmontalban
  • 351
  • 2
  • 10
  • This is awesome, now how do I go about getting the Average like pictured, rounded to 2 decimal places? – newcoder Mar 11 '19 at 04:07
  • @McKenzieLynnMeuleveld just add Math.Round(average,2); – justinmontalban Mar 11 '19 at 04:12
  • @justinmontalban you sir, have saved my life. THANK YOU for being so fast to respond and so useful, I appreciate it more than you know! – newcoder Mar 11 '19 at 04:15
  • I do, my second foreach loop was where I thought the problem was, but I couldnt figure out why! – newcoder Mar 11 '19 at 04:17
  • `Math.Round(average,2)` You may wish to briefly explain why `2.225` rounds to `2.22` but `2.235` rounds to `2.24` if you plan to use `Math.Round` like that. – mjwills Mar 11 '19 at 04:44
  • @brandnewcoder appreciate mate, I hope I was able to help you too. which one you couldn't figure it out? – justinmontalban Mar 11 '19 at 04:56
  • No, it doesn't do that. That is my point. People think 2.225 will round to 2.23. But it doesn't, unless you use https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?view=netframework-4.7.2 . – mjwills Mar 11 '19 at 05:46
0

Well your Loop is buggy.

Change it to something like below:

foreach (string name in table.Keys)
{
  List<int> value = table[name];
  Console.WriteLine($"{name} exam scores: {string.Join(" ", value)}");
}

Update (as its homework):

foreach (string name in table.Keys)
{
    List<int> value = table[name];
    string scoreDisplay = string.Empty;

    foreach (var score in value)
    {
        scoreDisplay += score + " ";
    }
    Console.WriteLine($"{name} exam scores: {scoreDisplay}");
}
Prateek Shrivastava
  • 1,877
  • 1
  • 10
  • 17