【问题标题】:Dictionary Issues [duplicate]字典问题[重复]
【发布时间】:2019-03-11 03:42:27
【问题描述】:

我应该做一个列表字典来显示不同人的测试分数,最终结果应该是这样的:

目前我的代码看起来像这样,我遇到的问题是数字打印了 3 次,而不是显示的一次。请帮忙!

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();
        }
    }
}

【问题讨论】:

  • 以文本形式显示预期结果和以文本形式显示实际结果。
  • 逐行调试。对于每个name,第二个foreach 输入了多少次?你认为这是为什么?
  • 为什么这个问题的代码被删掉了?
  • @brandnewcoder - 如果您还有其他问题,请提交一个新问题,而不是编辑这个问题。

标签: c# list dictionary


【解决方案1】:

试试这个,我首先使用的是 Console.Write 而不是 WriteLine,因此分数的等级将显示在名称的下一个。然后在 foreach 之后为下一个人添加 Console.WriteLine 将其打印到下一行。

使用 Math.Round(average,2) 意味着它将您的平均变量四舍五入到最接近的百分之一。

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

【讨论】:

  • 这太棒了,现在我该如何获得如图所示的平均值,四舍五入到小数点后 2 位?
  • @McKenzieLynnMeuleveld 只需添加 Math.Round(average,2);
  • @justinmontalban 你先生,救了我的命。感谢您如此快速的响应和如此有用,我比您知道的更感激它!
  • 我知道,我的第二个 foreach 循环是我认为问题所在,但我不知道为什么!
  • Math.Round(average,2) 您可能希望简要解释一下为什么 2.225 会舍入到 2.22 但如果您打算像这样使用 Math.Round2.235 会舍入到 2.24
【解决方案2】:

你的 Loop 有问题。

将其更改为如下所示:

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

更新(作为家庭作业):

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}");
}

【讨论】:

  • 我的字符串循环中应该有一个 for each 循环(编码作业)
  • 这行得通!现在我将如何获取每组的平均值并将其放在名称下,如图所示?
猜你喜欢
  • 2014-07-20
  • 2023-04-03
  • 1970-01-01
  • 2019-03-12
  • 2019-02-25
  • 2019-03-26
  • 1970-01-01
  • 2011-04-10
  • 2012-11-11
相关资源
最近更新 更多