五个候选班长,20人投票。(输入1-5来表示支持哪个人)最后查看票数,看那个人胜出

for (; ; )
{
Console.WriteLine("班长投票选举(1-5)");
Console.WriteLine("请选择你要投票的人:");
Console.WriteLine("1、张三");
Console.WriteLine("2、李四");
Console.WriteLine("3、王五");
Console.WriteLine("4、马六");
Console.WriteLine("5、冯七");
Console.WriteLine("投票开始!");
int[] sum = new int[5] { 0, 0, 0, 0, 0 };
int fei = 0;
int[] piao = new int[20];
for (int i = 0; i < 20; i++)
{
Console.Write("请" + (i + 1) + "个同学投票:");
piao[i] = int.Parse(Console.ReadLine());
switch (piao[i])
{
case 1: sum[0]++; break;
case 2: sum[1]++; break;
case 3: sum[2]++; break;
case 4: sum[3]++; break;
case 5: sum[4]++; break;
default: fei++; break;
}
}
Console.WriteLine("投票结果:");
Console.WriteLine("1、张三总票数:" + sum[0]);
Console.WriteLine("2、李四总票数:" + sum[1]);
Console.WriteLine("3、王五总票数:" + sum[2]);
Console.WriteLine("4、马六总票数:" + sum[3]);
Console.WriteLine("5、冯七总票数:" + sum[4]);
Console.WriteLine("弃权票:" + fei);
if (sum[0] > sum[1] && sum[0] > sum[2] && sum[0] > sum[3] && sum[0] > sum[4])
{
Console.WriteLine("张三当选为班长"); break;
}
else if (sum[1] > sum[0] && sum[1] > sum[2] && sum[1] > sum[3] && sum[1] > sum[4])
{
Console.WriteLine("李四当选为班长"); break;
}
else if (sum[2] > sum[1] && sum[2] > sum[0] && sum[2] > sum[3] && sum[2] > sum[4])
{
Console.WriteLine("当王五选为班长"); break;
}
else if (sum[3] > sum[1] && sum[3] > sum[2] && sum[3] > sum[0] && sum[3] > sum[4])
{
Console.WriteLine("马六当选为班长"); break;
}
else if (sum[4] > sum[1] && sum[4] > sum[2] && sum[4] > sum[3] && sum[4] > sum[0])
{
Console.WriteLine("冯七当选为班长"); break;
}
else if(sum[0]==sum[1]||sum[0]==sum[2]||sum[0]==sum[3]||sum[0]==sum[4]||sum[1]==sum[2]||sum[1]==sum[3]||sum[1]==sum[4]||sum[2]==sum[3]||sum[2]==sum[4]||sum[3]==sum[4])
{
Console.WriteLine("请重新投票!");
}
}
Console.ReadLine();

posted @ 2016-05-10 21:34  枫炎  阅读(422)  评论(0编辑  收藏  举报