在班级类中增加一个能计算班级平均年龄的方法,每个班输入第一个学生学号,其它学号自动生成,如01班第一个学生学号为09010101,则其它学生的学号依次为09010102~09010105。在主程序Main中建立3个班,每个班5个学生,读入相关数据,输出每个班及学生的有关信息和该班学生的平均年龄。下面是我写的程序,请高手为我补全赐教,要具体的程序代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Student
{
private string ID;
private string Name;
private string Sfzh;
private char Sex;
public string id
{
get
{
return ID;
}
}
public string name
{
get
{
return Name;
}
set
{
Name = value;
}
}
public string sfzh
{
get
{
return Sfzh;
}
}
public int age
{
get
{ int age = DateTime.Now.Year - int.Parse(Sfzh.Substring(6, 4));
return age;
}
set
{
age = value;
}
}
public char sex
{
get
{
return Sex;
}
set
{
Sex = value;
}
}
public Student(string name, string id, char sex,string sfzh)
{
Name = name;
ID = id;
Sex = sex;
Sfzh = sfzh;
}
public void Display()
{
Console.WriteLine("姓名{0},学号{1},性别{2},年龄{3}", Name, ID, Sex,age);
}
}
class Class
{
private string cID;
private string counsellor; //辅导员
private ArrayList stu; //学生数组
public string cid
{
get
{
return cID;
}
}
public string cname
{
get
{
return counsellor;
}
set
{
counsellor = value;
}
}
public Class(string cid, string cname)
{
cID = cid;
counsellor = cname;
}
public void CreateClass()
{
stu = new ArrayList();
string name, id,sfzh;
char sex;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("请输入第{0}个学生的信息:", i + 1);
Console.WriteLine("姓名:");
name = Console.ReadLine();
Console.WriteLine("学号:");
id = Console.ReadLine();
Console.WriteLine("性别:");
sex = char.Parse(Console.ReadLine());
Console.WriteLine("身份证号:");
sfzh = Console.ReadLine();
stu.Add(new Student(name, id, sex,sfzh));
}
}
public void Display()
{
Console.WriteLine("班号{0},辅导员{1}", cID, counsellor);
IEnumerator ie = stu.GetEnumerator();
while (ie.MoveNext())
((Student)ie.Current).Display();
}
}
}
class Program
{
static void Main(string[] args)
{
string cid, counsellor;
Console.WriteLine("请输入班号:");
cid = Console.ReadLine();
Console.WriteLine("请输入辅导员姓名:");
counsellor = Console.ReadLine();
Class s = new Class(cid, counsellor);
s.CreateClass();
s.Display();
}
}
}上面的程序应该具体怎么改,才能达到我想要的结果,求教啊,急!!!
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Student
{
private string ID;
private string Name;
private string Sfzh;
private char Sex;
public string id
{
get
{
return ID;
}
}
public string name
{
get
{
return Name;
}
set
{
Name = value;
}
}
public string sfzh
{
get
{
return Sfzh;
}
}
public int age
{
get
{ int age = DateTime.Now.Year - int.Parse(Sfzh.Substring(6, 4));
return age;
}
set
{
age = value;
}
}
public char sex
{
get
{
return Sex;
}
set
{
Sex = value;
}
}
public Student(string name, string id, char sex,string sfzh)
{
Name = name;
ID = id;
Sex = sex;
Sfzh = sfzh;
}
public void Display()
{
Console.WriteLine("姓名{0},学号{1},性别{2},年龄{3}", Name, ID, Sex,age);
}
}
class Class
{
private string cID;
private string counsellor; //辅导员
private ArrayList stu; //学生数组
public string cid
{
get
{
return cID;
}
}
public string cname
{
get
{
return counsellor;
}
set
{
counsellor = value;
}
}
public Class(string cid, string cname)
{
cID = cid;
counsellor = cname;
}
public void CreateClass()
{
stu = new ArrayList();
string name, id,sfzh;
char sex;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("请输入第{0}个学生的信息:", i + 1);
Console.WriteLine("姓名:");
name = Console.ReadLine();
Console.WriteLine("学号:");
id = Console.ReadLine();
Console.WriteLine("性别:");
sex = char.Parse(Console.ReadLine());
Console.WriteLine("身份证号:");
sfzh = Console.ReadLine();
stu.Add(new Student(name, id, sex,sfzh));
}
}
public void Display()
{
Console.WriteLine("班号{0},辅导员{1}", cID, counsellor);
IEnumerator ie = stu.GetEnumerator();
while (ie.MoveNext())
((Student)ie.Current).Display();
}
}
}
class Program
{
static void Main(string[] args)
{
string cid, counsellor;
Console.WriteLine("请输入班号:");
cid = Console.ReadLine();
Console.WriteLine("请输入辅导员姓名:");
counsellor = Console.ReadLine();
Class s = new Class(cid, counsellor);
s.CreateClass();
s.Display();
}
}
}上面的程序应该具体怎么改,才能达到我想要的结果,求教啊,急!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货