using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Student { public string name; //字段 名字 public int age; //字段 年龄 public int _class; //字段 班级 public long number; //字段 学号 public Student() //构造方法一 { this.name = "涨三"; //赋默认值 this.age = 11; this._class = 2017; this.number = 0000; } public Student(string F2Name, int F2Age, int F2_class, long F2Number) //构造方法二 { this.name = F2Name; //实参赋值给相应字段 this.age = F2Age; this._class = F2_class; this.number = F2Number; } } class Program { static void Main(string[] args) { long sum = 0; const int SIZE = 5; Student[] a = new Student[SIZE]; //类数组 for (int i = 0; i < SIZE; i++) { a[i] = new Student(); //数组声明 Console.WriteLine("第{0}次输入数据:\n名字", i + 1); a[i].name=Console.ReadLine(); //数组实例化 Console.WriteLine("年龄"); a[i].age= int.Parse(Console.ReadLine()); Console.WriteLine("班级"); a[i]._class= int.Parse(Console.ReadLine()); Console.WriteLine("学号"); a[i].number= long.Parse(Console.ReadLine()); Student c = new Student(a[i].name, a[i].age, a[i]._class, a[i].number); } for (int i=0;i<SIZE;i++) { sum += a[i].age; //调用10个不同对象的age } Console.WriteLine(sum/SIZE); Console.Read(); } } }
【c#】教材page77项目实训
猜你喜欢
转载自blog.csdn.net/qq_41138935/article/details/80159026
今日推荐
周排行