我需要能够显示每年有多少注册学生。所以我有一个名为School的课程,其中学生在课堂上创建的对象可以注册到特定的学校,不知道如何做到这一点。我试图学习OOP,因为我没有在Uni中这样做并且必须教它。我知道这可能是一个简单的问题,但现在已经尝试了一周,而且很简单。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace objects2
{
class Program
{
static void Main()
{
Student jack = new Student();
jack.setName("Jack");
jack.setStudentNumber(1);
jack.setYearLevel(13);
Student jane = new Student();
jane.setName("Jane");
jane.setStudentNumber(2);
jane.setYearLevel(10);
Student Amy = new Student();
Amy.setName("Amy");
Amy.setStudentNumber(3);
Amy.setYearLevel(10);
Student Ezekeil = new Student();
Ezekeil.setName("Ezekiel");
Ezekeil.setStudentNumber(4);
Ezekeil.setYearLevel(9);
Student Burt = new Student();
Burt.setName("Burt");
Burt.setStudentNumber(5);
Burt.setYearLevel(9);
Student Ernie = new Student();
Ernie.setName("Ernie");
Ernie.setStudentNumber(6);
Ernie.setYearLevel(11);
Student James = new Student();
James.setName("James");
James.setStudentNumber(7);
James.setYearLevel(11);
School CHBC = new School();
CHBC.enrol(Amy);
CHBC.enrol(jack);
CHBC.enrol(jane);
CHBC.enrol(Ezekeil);
CHBC.enrol(Burt);
CHBC.enrol(Ernie);
Console.WriteLine(" Number enrolled: " + CHBC.countEnrolled);
Console.ReadKey();
}
}
class Student
{
private string name;
private int studentNumber;
private int yearLevel = 9;
public int setYearLevel(int yearLevel)
{
if (yearLevel <= 13 && yearLevel >= 9)
{
//talking about the variable in the class by using this to stop having to reinvent names
this.yearLevel = yearLevel;
return 0;
}
//error codes so can have specific error otherwise could use boolean for OK or not OK
return -1;
}
public int showYearLevel()
{
return yearLevel;
}
public void setStudentNumber(int studentNumber)
{
this.studentNumber = studentNumber;
}
public int showStudentNumber()
{
return studentNumber;
}
public void setName(string name)
{
this.name = name;
}
public string showName()
{
return name;
}
}
class School
{
private string schoolName;
private string type;
private List<Student> students = new List<Student>;
private int numberEnrolled;
public void enrol(Student newStudent)
{
//add students to my list as they arre enrolled
students.Add(newStudent);
}
public void setSchoolName(string schoolName)
{
this.schoolName = schoolName;
}
public int countEnrolled
{
get
{
numberEnrolled = students.Count();
return numberEnrolled;
}
}
public void setType(string type)
{
this.type = type;
}
}
}
提前致谢。
答案 0 :(得分:0)
那Student
课很可怕。从这样的事情开始:
class Student
{
public int Number { get; private set; }
public string Name { get; private set; }
public int YearLevel { get; private set; }
const int yearLevelMin = 9;
const int yearLevelMax = 13;
public Student(int number, string name, int yearlevel)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (yearlevel < yearLevelMin || yearlevel > yearLevelMax)
throw new StudentException($"{nameof(yearlevel)} must be between {yearLevelMin} and {yearLevelMax}.");
Number = number;
Name = name;
YearLevel = yearlevel;
}
}
让您的基础课程先行。我不会为你写School
课程,但你应该可以这样做:
var school = new School("Example High")
.Add(new Student(1, "Jack", 13))
.Add(new Student(2, "Jane", 10));
答案 1 :(得分:0)
我将跳过一些细节,然后直接回答你的问题。
首先,您需要拥有属性的公共访问者。
public class Student
{
public int Number { get; set; }
public string Name { get; set; }
public int YearLevel { get; set; }
}
public class School
{
public string Name { get; set; }
public string Type { get; set; }
public List<Student> Students { get; set; }
}
然后您可以按年级计算学生人数。
// this is using Linq
int numberOfStudents = Students.Count(s=>s.YearLevel == yearLevelYouAreLooking);
但是你想要显示每年级别的计数,所以你需要的是按年级对学生进行分组并获得计数。
// school is School instance
var result = from student in school.Students // from all students in the school
group student by student.YearLevel into g // group the students by the YearLevel
select new { YearLevel = g.Key, Count = g.Count() } // create a new list of YearLevel and the count.
将结果显示在控制台
中foreach(var item in result)
{
Console.WriteLine($"Number of students of {item.YearLevel} is {item.Count}");
}
您可以根据需要将它们作为课程的方法。
注意:结果不在订单中。如果您需要订购结果,请添加:
orderby student.YearLevel // in between "from" and "group", but this is not optimal, because you are re-ordering the input first, which can be large.
或
orderby g.Key // in between "group" and "select". Since the grouped result will be smaller than the input, ordering is optimal.