您的位置:首页 > 其它

投票选班长

2015-07-14 21:40 549 查看
static void Main46投票选班长(string[] args)
{
////某班有20个学生,投票选班长,总共有5个候选人,实现投票,并且计算出得票最多的人以及他的票数。

int[] tp = new int[20]; //每个人投的票
int[] ps = new int[5]; //每个候选人所得票数

for (int i = 0; i < tp.Length; i++)
{
Console.WriteLine("请投票,1代表第一个候选人,2代表第二个候选人--5带表第五个候选人");
tp[i] = Convert.ToInt32(Console.ReadLine());
}

for (int j = 0; j < tp.Length; j++)
{
if (tp[j] == 1)
{
ps[0] = ps[0] + 1;
}
else if (tp[j] == 2)
{
ps[1] = ps[1] + 1;
}
else if (tp[j] == 3)
{
ps[2] = ps[2] + 1;
}
else if (tp[j] == 4)
{
ps[3] = ps[3] + 1;
}
else if (tp[j] == 5)
{
ps[4] = ps[4] + 1;
}
}

//看一下谁得的票数最多
int max = ps[0]; //找出最大的
for (int x = 1; x < ps.Length; x++)
{
if (max < ps[x])
{
max = ps[x];
}
}
//找最大的索引
int[] sy = new int[5];
for (int y = 0; y < ps.Length; y++)
{
if (ps[y] == max)
{
sy[y] = y + 1;
}
}

//输出

Console.WriteLine("所得票数最多为:" + max);
Console.WriteLine("所得票数最多的人为:");
for (int z = 0; z < sy.Length; z++)
{
if (sy[z] == 0)
{
}
else
{
Console.WriteLine(sy[z]);
}
}

Console.ReadLine();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: