c# - 如何在没有 LINQ 的情况下合并两个字典的值?

标签 c# list dictionary console-application

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

我被指示让它在没有任何 LINQ 运算符的情况下工作。

这是我的两本词典:

Dictionary<int, string> ballots = new Dictionary<int, string>();
Dictionary<int, int> votes = new Dictionary<int, int>();

这是我要求投票的方式:

//Print the ballots as list
Console.WriteLine("Voting Ballots:");
foreach (KeyValuePair<int, string> ballot in ballots)
{
    Console.WriteLine("{0} - {1}", ballot.Key, ballot.Value);
}
//Voting process
Console.WriteLine("\nVote for one of the ballots");
Console.WriteLine("---------------------------------");
Console.WriteLine("Write the number of the ballot you want to vote for: ");
int nVote; int.TryParse(Console.ReadLine(), out nVote);
int val;
string nBallot;
//Verify if the ballot exists
if (planillas.TryGetValue(nVote, out nBallot)){
    Console.WriteLine("You've voted for the ballot {0}", nBallot);
    if (votes.TryGetValue(nVote, out val)) {
        votes[nVote] += 1;
    }
}else{
      Console.WriteLine("The ballot #{0} doesn't exist. \nPlease try again.", nVote);
}

我需要输出结果如下:

BALLOT ID --------- BALLOT NAME ------------ NUMBER OF VOTES

   1      ------  BALLOT NAME   ---------       5

   2      ------  BALLOT NAME   ---------       15

   3      ------  BALLOT NAME   ---------       25

选票 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);
}

我试过这样做:

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  

在那个结果中,我三次投了第三票。然而,它正在工作......它正在打印破折号 (----------) 和根据 ballots-1 的数量相应的票数。

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

最佳答案

首先,我建议更新您的代码以获得 Dictionary<int, Ballot>其中 Ballotclass有名字和计数,所以你不需要管理两个字典。不管...

您当前的问题是 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);
}

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

关于c# - 如何在没有 LINQ 的情况下合并两个字典的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347005/

相关文章:

c# - 使用 AutoFixture 创建谓词表达式

python - 展平列表和标量列表

python通过包含几个键值对作为条件的dict过滤dict列表

ios - 使用嵌套对象和键为数组建模

python - 排序包含列表的字典

c# - 如何通过混合值和键将字典转换为列表?

c# - 三种地址的正则表达式

c# - Nest 6.2.0自引用循环

c# - List<int[]> 仅由 int[] 的最后内容填充

python - 修复 Python 中的错误?