c# - 如何在没有 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相对应。
我怎样才能得到我想要的结果?
解决方案
在任何事情之前,我建议更新您的代码以使Dictionary<int, Ballot>
where Ballot
is 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 和票数但没有选票名称的报告条目。
推荐阅读
- angular - 如果使用 ngIf 隐藏字段,如何从角度反应形式切换(添加/删除)表单控件?
- c++ - 链表中双指针的使用
- react-spring - 如何在单个组件上附加多个反应弹簧?
- c - c语言中的面试题reg二进制位操作
- docker - 如何从 docker-compose 或 docker run 写入现有的填充卷
- javascript - 根据日期从数据库中获取唯一值,当用户单击日期时,然后展开数据库中可用的详细信息
- javascript - 如何通过同一行的另一列的一个文本框中的输入获取值
- flutter - 如何在 vscode 中禁用“运行|调试”行?
- javascript - 使用 fetch API 和 NodeJS(express)
- python - 在pygame中出现游戏结束屏幕后重新启动蛇游戏