C#利用循环生成10个学生对象,求所有学生的平均年龄
2个回答
展开全部
类定义:
public class Student
{
public int Age { get; set; }
}
代码:
ArrayList alStu = new ArrayList();
for (int i = 1; i <= 10; i++)
{
Student _stu = new Student();
_stu.Age = i;
alStu.Add(_stu);
}
int _iAvgAge = 0;
int _iSum = 0;
foreach (object _stuItem in alStu)
{
Student _tmp = (Student)_stuItem;
_iSum += _tmp.Age;
}
_iAvgAge = _iSum / alStu.Count;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询