首页 > 解决方案 > 如何在没有 LINQ 的情况下组合两个字典的值?

问题描述

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

我被指示在没有任何 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);
}

我需要输出结果如下:

选票 ID --------- 选票名称 ------------ 票数

   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  

在那个结果中,我为第三号选票投了三票。然而,它正在工作......它正在打印破折号(----------)和投票数与投票数-1相对应。

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

标签: c#listdictionaryconsole-application

解决方案


在任何事情之前,我建议更新您的代码以使Dictionary<int, Ballot>where Ballotis aclass具有名称和计数,这样您就不需要管理两个字典。不管...

您当前的问题是另一个循环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 和票数但没有选票名称的报告条目。


推荐阅读