下面我有兩個類的建構式。學生和課程
我想的值進行比較_completedCourses值從對學生類coursecode課程班。
_completedCourses是課程及其成績的字典。我正在嘗試查找未完成的課程并列印該串列。
我這樣做是否正確。
public Course(string coursName, string courseCode, char passingGrade, double noOfCredits, Semester semesterOfferd, int major)
{
this.CourseName = coursName;
this.CourseCode = courseCode;
this.PassingGrade = passingGrade;
this.NoOfCredits = noOfCredits;
this.SemesterOfferd = semesterOfferd;
this.prerequisiteCourses = new List<Course>();
this._enrolledStudents = new List<Student>();
this._major = major;
}
public Student(int studentID, string studentName, Status studentStatus, StudentMajor studentMajor)
{
this._studentID = studentID;
this._studentName = studentName;
this._studentStatus = studentStatus;
this._studentMajor = studentMajor;
this._course = new Course();
this._completedCourses = new Dictionary<string, char>();
countStudent ;
}
public void RemainingCourses()
{
var cKey = _completedCourses.ContainsKey(_course.CourseCode);
Console.WriteLine("Remaining Courses");
if (cKey.Equals(_course.CourseCode))
{
foreach (KeyValuePair<string, char> count in _completedCourses)
{
{
{
Console.WriteLine(count.Key " " count.Value);
// Console.WriteLine("Course " count.Value);
count.ToString();
}
}
}
}
}
更新!!!
我的驅動程式類中的以下代碼行實作了我想要的
Console.WriteLine("Enter Student ID ");
input = Convert.ToInt32(Console.ReadLine());
Console.Clear();
if (input.Equals(id.StudentID)) {
id.DisplayCompletedCourse();
foreach (var sub in isdCourses) {
var cKey = id.CompletedCourses.ContainsKey(sub.CourseCode);
if (!cKey)
{
Console.WriteLine(sub.CourseCode);
}
}
}
uj5u.com熱心網友回復:
該ContainsKey方法回傳一個布林值(真/假),所以在你的 if 條件中:
if (cKey.Equals(_course.CourseCode))
cKey是一個布林值但是_course.CourseCode是一個字串,所以這永遠不會是真的,因此這永遠不會進入 if 塊。
嘗試像這樣重寫它:
public void RemainingCourses()
{
var cKey = _completedCourses.ContainsKey(_course.CourseCode);
Console.WriteLine("Remaining Courses");
if (cKey) // if cKey is "true", then the dictionary contains the CourseCode
{
foreach (KeyValuePair<string, char> count in _completedCourses)
{
Console.WriteLine(count.Key " " count.Value);
}
}
}
作為旁注,請注意格式,您正在添加不必要的花括號。此外,這可以使用 linq 進行簡化,但我盡可能地將其與原始代碼保持一致。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400156.html
上一篇:隨機選擇串列
