Я создаю программу экспресс-опроса.
Мне дали указание заставить ее работать без каких-либо операторов 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 ------------ КОЛИЧЕСТВО ГОЛОСОВАНИЙ
1 ------ BALLOT NAME --------- 5
2 ------ BALLOT NAME --------- 15
3 ------ BALLOT NAME --------- 25
Идентификатор бюллетеня - это заданное число, а также имя.
Я печатаюРезультаты выглядят следующим образом:
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.
Как я могу получить желаемый результат