所以我知道如何计算平均值,但我不知道如何计算最小值和最大值。我很确定你需要使用for循环,但我不知道如何。
public Int32 Average
{
get
{
clsSimpleDataConnection Datalayer = new clsSimpleDataConnection();
List<Int32> saleslist = new List<Int32>();
saleslist = Datalayer.LoadIntegerList();
Int32 AnItem;
Int32 Total = 0;
Int32 average;
Int32 itemcount;
itemcount = saleslist.Count;
Int32 index = 0;
while (index < itemcount)
{
AnItem = saleslist[index];
Total = Total + AnItem;
index++;
}
average = Total / itemcount;
return average;
}
}
我如何编辑它以便计算最小值和最大值?
答案 0 :(得分:0)
var myList = new List<int>();
var min = myList.Min();
var max = myList.Max();
或者如果你想使用循环 所以对于max
int max = int.MinValue;
foreach (var type in myList)
{
if (type > max)
{
max = type;
}
}
和min
int min = int.MaxValue;
foreach (var type in myList)
{
if (type < min)
{
min= type;
}
}