我有一个学生名单和一个商标名单。我想根据输入的ID添加学生标记,然后查看记录。我没有正确实现它。有人可以告诉我如何正确地做到这一点吗? 这是我想要实现的输出: 1)加入学生 2)添加标记:将要求用户输入id,如果存在,则根据id,然后根据该id存储标记。 3)查看标记:查看学生ID和他的标记
class Student
{
public Student(string name, string surname, string dob, string address)
{
this.Name = name;
this.Surname = surname;
this.DOB = dob;
this.Address = address;
}
public List<Marks> studentId = new List<Marks>();
public List<Marks> Mathematics = new List<Marks>();
public List<Marks> English = new List<Marks>();
public List<Marks> Maltese = new List<Marks>();
public List<Marks> ReligiousStudies = new List<Marks>();
public List<Marks> SocialStudies = new List<Marks>();
public string Name { get; set; }
public string Surname { get; set; }
public string DOB { get; set; }
public string Addr { get; set; }
public string Id { get; set; }
public Student(string s)
{
string[] splitted = s.Split(',');
name = splitted[0];
surname = splitted[1];
dob = splitted[2];
address = splitted[3];
id = splitted[4];
}
public void AddMarks(string id, int hyexam, int anexam)
{
if (Id.Equals(id))
{
studentId.Add(new Marks(id));
Mathematics.Add(new Marks(hyexam, anexam));
English.Add(new Marks(hyexam, anexam));
Maltese.Add(new Marks(hyexam, anexam));
ReligiousStudies.Add(new Marks(hyexam, anexam));
SocialStudies.Add(new Marks(hyexam, anexam));
}
else
{
Console.WriteLine("id not found");
}
}
public void StudMarks()
{
foreach (Marks mrk in studentId)
{
Console.WriteLine(mrk);
}
foreach (Marks mrk in Mathematics)
{
Console.WriteLine(mrk);
}
foreach (Marks mrk in English)
{
Console.WriteLine(mrk);
}
foreach (Marks mrk in Maltese)
{
Console.WriteLine(mrk);
}
foreach (Marks mrk in ReligiousStudies)
{
Console.WriteLine(mrk);
}
}
}
}
public class ClassYear
{
private const string filestud = "C:\\Users\\Maria\\Desktop\\Students\\stud.txt";
public string Year { get; set; }
public List<Student> mystudent=new List <Student> ();
public List<Student> MyStud
{
get { return mystudent; }
}
public void Addstudent(string name,string surname,string dob, string addr)
{
mystudent.Add(new Student(name,surname,dob,addr));
}
private void LoadFromFile()
{
try
{
string s;
StreamReader myFile = File.OpenText(filestud);
while ((s = myFile.ReadLine()) != null)
{
Student st = new Student(s);
mystudent.Add(st);
}
myFile.Close();
}
catch (Exception e)
{
Console.WriteLine("There was a file handling problem");
}
}
private void DumpToFile()
{
try
{
StreamWriter myFile = File.CreateText(filestud);
foreach (Student s in mystudent)
{
myFile.WriteLine(s.ToString());
}
myFile.Close();
}
catch (Exception e)
{
Console.WriteLine("There was a file handling problem");
}
}
private int SearchItem(int search)
{
int found = -1;
if (mystudent != null)
{
foreach (Student st in mystudent)
{
found++;
if (Student.Equals(search,st)) {
break;
}
}
}
return found;
}
private void Delete(int index)
{
SearchItem(index);
mystudent.RemoveAt(index);
foreach (Student st in mystudent) {
Console.WriteLine(st);
}
}
private void EditItem(int index)
{
Student stud = new Student();
SearchItem(index);
mystudent[index] = stud;
Console.WriteLine("current record is" + index);
Console.WriteLine("enter new record:");
stud.Name = Console.ReadLine();
stud.Surname = Console.ReadLine();
stud.Addr = Console.ReadLine();
stud.Id = Console.ReadLine();
foreach (Student st in mystudent)
{
Console.WriteLine(st);
}
}
private void ViewStudents()
{
foreach (Student st in mystudent)
{
Console.WriteLine(st);
}
}
}
}
public class Marks
{
public int HYEXAM { get; private set; }
public int ANEXAM { get; private set; }
public string Id { get; private set; }
public Marks(int hyexam, int anexam)
{
this.HYEXAM = hyexam;
this.ANEXAM = anexam;
}
public Marks(string id)
{
this.Id = id;
}
public double OverallExam()
{
return (0.4 * HEYXAM) + (0.6 * ANEXAM);
}
}
}
答案 0 :(得分:1)
好的,据我所知,你有一份学生名单。这些学生中的每一个都有几个标记列表:数学,英语,马耳他语等。
因此,假设您要为某个学生添加数学标记。假设你有:
List<Student> students;
int userId; // the id entered by the user
Mark mathMark; // the mark you want to add
在列表中搜索给定的ID:
foreach (Student myStudent in students)
{
if (myStudent.Id == userId)
{
myStudent.Mathematics.Add(mathMark);
}
}
然后,当您想要显示学生的所有数学分数时:
foreach (Mark mark in myStudent.Mathematics)
{
Console.WriteLine(mark.HYEXAM + "," + mark.ANEXAM);
}
这就是你要追求的吗?