编程实践5-7
编程实践5-7
课程元素类型 任务
统计选票。100位村民投票,从3位候选人中选出村长和村支书。规定得票最高者为村长,次高者为村支书。统计时,输入得票候选人的姓氏,输出投票结果。
总结:大于两个的数据,尽量用数组,写程序要考虑到拓展,要程序拥有良好的拓展性。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sj5_7 { class Program { static void Main(string[] args) { string strTem; string[] strB; int[] intA; int intTem; intA = new int[3]; strB = new string[3]; strB[0] = "Z"; strB[1] = "L"; strB[2] = "W"; Console.WriteLine("请输入投票:"); strTem = Console.ReadLine(); string[] strA = strTem.Split(' '); for (int i = 0; i < strB.Length; i++) { for (int j = 0; j < strA.Length; j++) { if (strB[i] == strA[j]) { intA[i]++; } } } for (int i = 0; i <intA.Length; i++) { for (int j = i + 1; j < intA.Length; j++) { if (intA[i] < intA[j]) { intTem = intA[i]; intA[i] = intA[j]; intA[j] = intTem; strTem = strB[i]; strB[i] = strB[j]; strB[j] = strTem; } } } Console.WriteLine("村长是:{0},得票{1}",strB[0],intA[0]); Console.WriteLine("村支书是:{0},得票{1}", strB[1], intA[1]); Console.ReadKey(true); } } }
浙公网安备 33010602011771号