:向集合中添加10个元素,计算平均值,求最大、最小值。
1 double sum = 0; 2 list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); 3 int Max, Min; 4 Max = Min = (int)list[0]; 5 6 for (int i = 0; i < list.Count; i++) 7 { 8 //最大值 9 if(Max < (int)list[i]) 10 { 11 Max = (int)list[i + 1]; 12 } 13 //最小值 14 else if (Min > (int)list[i]) 15 { 16 Min = (int)list[i]; 17 } 18 else { } 19 sum += Convert.ToDouble(list[i]); 20 } 21 for (int i = 0; i < list.Count; i++) 22 { 23 Console.Write(list[i] + " "); 24 } 25 Console.WriteLine("\nAvg = {0}",sum / list.Count); 26 Console.WriteLine("Max = {0}", Max); 27 Console.WriteLine("Min = {0}", Min);
:把10个随机数存储到集合中,且不重复
1 Random rd = new Random(); 2 for (int i = 0; i < 10; i++) 3 { 4 int n = rd.Next(0, 10); 5 if (!list.Contains(n)) 6 { 7 list.Add(n); 8 } 9 } 10 for (int i = 0; i < list.Count; i++) 11 { 12 Console.WriteLine(list[i]); 13 }
完成。
时间: 2024-10-10 06:07:03