:向集合中添加10个元素,计算平均值,求最大、最小值。
double sum = ;
list.AddRange(new int[] { , , , , , , , , });
int Max, Min;
Max = Min = (int)list[]; for (int i = ; i < list.Count; i++)
{
//最大值
if(Max < (int)list[i])
{
Max = (int)list[i + ];
}
//最小值
else if (Min > (int)list[i])
{
Min = (int)list[i];
}
else { }
sum += Convert.ToDouble(list[i]);
}
for (int i = ; i < list.Count; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine("\nAvg = {0}",sum / list.Count);
Console.WriteLine("Max = {0}", Max);
Console.WriteLine("Min = {0}", Min);
:把10个随机数存储到集合中,且不重复
Random rd = new Random();
for (int i = ; i < ; i++)
{
int n = rd.Next(, );
if (!list.Contains(n))
{
list.Add(n);
}
}
for (int i = ; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
完成。