Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有LINQ的情况下组合两个字典的值?_C#_List_Dictionary_Console Application - Fatal编程技术网

C# 如何在没有LINQ的情况下组合两个字典的值?

C# 如何在没有LINQ的情况下组合两个字典的值?,c#,list,dictionary,console-application,C#,List,Dictionary,Console Application,我正在创建一个快速投票程序 我被指示在没有任何LINQ操作员的情况下使其工作 这是我的两本字典: Dictionary<int, string> ballots = new Dictionary<int, string>(); Dictionary<int, int> votes = new Dictionary<int, int>(); 选票ID是一个给定的数字,也是一个名字 我将结果打印如下: Console.Clear(); Console

我正在创建一个快速投票程序

我被指示在没有任何LINQ操作员的情况下使其工作

这是我的两本字典:

Dictionary<int, string> ballots = new Dictionary<int, string>();
Dictionary<int, int> votes = new Dictionary<int, int>();
选票ID是一个给定的数字,也是一个名字

我将结果打印如下:

Console.Clear();
Console.WriteLine("VOTING RESULTS: ");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("BALLOT ID --------- BALLOT NAME ------------ NUMBER OF VOTES");
List<int> nVotes = votes .Keys.ToList<int>();
foreach (KeyValuePair<int, int> ballot in votes )
{
    Console.Write("{0} ----- ", ballot.Key);
     // I need to print the ballot's name here
    Console.Write("----- {0}", ballot.Value);
}
结果,我三次投票支持第三号选票。不过,它正在发挥作用。。。它正在打印破折号(------------)和票数,相应的票数为-1


我怎样才能得到我想要的结果呢?

在做任何事情之前,我建议更新你的代码,让它有一个
字典
,其中
Ballot
是一个
类,有一个名称和计数,所以你不需要管理两个字典。不管怎样

您当前的问题是另一个
foreach
循环中的
foreach
循环。如果保证两个字典中的键相同,则可以删除第二个循环:

foreach (KeyValuePair<int, int> planilla in votosPlanillas)
{
    Console.Write("\n ---------- {0} ---------- ", planilla.Key);
    if (planillas.TryGetValue(planilla.Key, out nPlanilla)){
        Console.Write("{0} ", nPlanilla);
    }

    Console.Write(" ---------- {0}", planilla.Value);
}
foreach(votosPlanillas中的KeyValuePair planilla)
{
Write(“\n------{0}------”,planilla.Key);
if(planilla.TryGetValue(planilla.Key,out nPlanilla)){
Console.Write(“{0}”,nPlanilla);
}
Write(“------------{0}”,planilla.Value);
}

请记住,如果没有正确同步词典,则
if
语句将失败,您将得到一个报告条目,其中包含ID和投票数,但没有投票名称。

您推荐
struct
的具体原因是什么?对于初学者来说,很少有好的选择。@AlexeiLevenkov我想
class
会更好,因为票数可以经常增加。我会改变我回答的措辞;谢谢你指出这一点。
Console.Clear();
Console.WriteLine("VOTING RESULTS: ");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("BALLOT ID --------- BALLOT NAME ------------ NUMBER OF VOTES");
List<int> nVotes = votes .Keys.ToList<int>();
foreach (KeyValuePair<int, int> ballot in votes )
{
    Console.Write("{0} ----- ", ballot.Key);
     // I need to print the ballot's name here
    Console.Write("----- {0}", ballot.Value);
}
foreach (KeyValuePair<int, int> planilla in votosPlanillas)
{
    Console.Write("\n ---------- {0} ---------- ", planilla.Key);
    if (planillas.TryGetValue(planilla.Key, out nPlanilla)){
        Console.Write("{0} ", nPlanilla);
    }
    foreach (KeyValuePair<int, string> namePlanilla in planillas) {
     Console.Write(" ---------- {0}", planilla.Value);
    }
}
 ----- BALLOT ID ----- BALLOT NAME ----- NUMBER OF VOTES
                                                                                                                                 
 ---------- 1 ---------- a  ---------- 1 ---------- 1 ---------- 1 ---------- 1                                                  
 ---------- 2 ---------- b  ---------- 1 ---------- 1 ---------- 1 ---------- 1                                                  
 ---------- 3 ---------- c  ---------- 3 ---------- 3 ---------- 3 ---------- 3                                                  
 ---------- 4 ---------- d  ---------- 1 ---------- 1 ---------- 1 ---------- 1  
foreach (KeyValuePair<int, int> planilla in votosPlanillas)
{
    Console.Write("\n ---------- {0} ---------- ", planilla.Key);
    if (planillas.TryGetValue(planilla.Key, out nPlanilla)){
        Console.Write("{0} ", nPlanilla);
    }

    Console.Write(" ---------- {0}", planilla.Value);
}