迭代列表中的所有元素

本文关键字:元素 列表 迭代 | 更新日期: 2023-09-27 17:59:53

在过去的两周里,我一直在做一项任务,我正在努力克服这个问题。

下面是代码。这绝不是一个完整的代码,不管怎样,一旦我把所有的部分都工作好了,我就会把它整理好。在Main中,情况1:在问了一系列问题后,我想将S1传递给R1.addStudent方法,这是有效的,但是,如果你查看注册类,addStudent方法会添加S1对象/数据成员并标记到列表中。

我的问题是,一旦我尝试迭代这个列表(请参阅Module类),它就会迭代元素的数量,但会两次返回最后一个条目名称(使用toString()方法),返回student。Name(从Person类继承的数据成员)。

我想让它迭代添加到列表中的实际名称,我该如何实现这一点?

请原谅冗长的解释,不要因为代码不整洁而大发雷霆,我会解决的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace P3_O_O_P
{
    public class Module
    {
        private string moduleName;
        private double moduleCode;
        public List<Registration> registerList = new List<Registration>();
        public Module(string newModuleName, double newModuleCode)
        {
            moduleName = newModuleName;
            moduleCode = newModuleCode;
        }
        public Module()
        {
        }
        public void Enrol(Student student, int mark, Classroom objClassroom)
        {
            Console.Clear();
            Console.Write("'nBelow are a list of modules that are currently available: 'n");
            Console.Write("'n1) Software Development'n");
            Console.Write("'n2) Chemistry'n");
            Console.Write("'n3) Science'n");
            Console.Write("'n4) Biology'n");
            Console.Write("'nSelection: ");
            int choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    objClassroom.addStudentProgramming(student, mark);
                    break;
                case 2:
                    objClassroom.addStudentChemistry(student, mark);
                    break;
                case 3:
                    objClassroom.addStudentScience(student, mark);
                    break;
                case 4:
                    objClassroom.addStudentBiology(student, mark);
                    break;
            }
        }
        public void studentListMarks()
        {
            for (int i = 0; i < registerList.Count; i++)
            {
                Console.WriteLine("'nStudent name: " + registerList[i].ToString() + "'nCurent student marks" + registerList[i].Mark);
            }
        }
        public string ModuleName
        {
            get
            {
                return moduleName;
            }
            set
            {
                moduleName = value;
            }
        }
        public double ModuleID
        {
            get
            {
                return moduleCode;
            }
            set
            {
                moduleCode = value;
            }
        }
        public void RemoveStudent()
        {
            throw new System.NotImplementedException();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace P3_O_O_P
{
    public class Registration
    {
        Student student; //Links student to registration, forcing data-integrity and granting a link to its data-members.
        private int mark;
        public Registration(Student ST, int mark) //passes the student object to the constructor.
        {
            this.mark = mark;
            student = ST; //Initializes the student object. allocates memory.
        }
        public int Mark
        {
            get
            {
                return mark;
            }
            set
            {
                mark = value;
            }
        }
        public int updateMark()
        {
            Console.Write("Please enter a new mark: ");
            mark = Convert.ToInt16(Console.ReadLine());
            return mark;
        }
        public void viewStudentMark()
        {
            Console.Write("'nStudent mark = : " + Mark);
        }
        public void addStudent(Student S1, Module objMod, Classroom objClassroom, int mark)
        {
            objMod.registerList.Add(new Registration(S1, mark));
            objMod.Enrol(S1, Mark, objClassroom);
        }
        public string studentSubject()
        {
            return student.Subject;
        }
        public string ToString(Student S1)
        {
            return student.Name;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace P3_O_O_P
{
    class Execute
    {
        static void Main(string[] args)
        {
            bool repeatMenu = true;
            int choice = 0;
            int mark = 0;
            Student S1 = new Student();
            Registration R1 = new Registration(S1, mark);
            Module M1 = new Module();
            Classroom C1 = new Classroom();
            do
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.BackgroundColor = ConsoleColor.White;
                Console.WriteLine("'n1) Enrol a student");
                Console.WriteLine("'n2) View students in Programming");
                Console.WriteLine("'n3) View students in Chemistry");
                Console.WriteLine("'n4) View students in Science");
                Console.WriteLine("'n5) View students in Biology");
                Console.WriteLine("'n6) View Classrooms (Modules)");
                Console.WriteLine("'n7) View all Students");
                Console.WriteLine("'n8) Update student mark ");
                Console.WriteLine("'n9) View student mark");
                Console.WriteLine("'n10) Exit Application");
                Console.Write("'nSelection: ");
                choice = Convert.ToInt32(Console.ReadLine());
                repeatMenu = false;
                Console.Clear();
                switch (choice)
                {
                    case 1:
                        Console.Write("'nPlease begin by entering your name: ");
                        S1.Name = Console.ReadLine();
                        Console.Write("'nPlease enter your Date of Birth: ");
                        S1.StudentDOB = Convert.ToDouble(Console.ReadLine());
                        Console.Write("'nPlease enter your preferred subject: ");
                        S1.Subject = Console.ReadLine();
                        Console.Write("'nPlease hand the console to the administrator, thank you.'n");
                        Console.Write("'nPlease enter/allocate an I.D.: ");
                        S1.ID = Convert.ToDouble(Console.ReadLine());
                        Console.Write("'nPlease enter a mark: ");
                        mark = Convert.ToInt32(Console.ReadLine());
                        R1.addStudent(S1, M1, C1, mark);
                        repeatMenu = true;
                        break;
                    case 2:
                        C1.ViewStudentsProgramming();
                        repeatMenu = true;
                        break;
                    case 3:
                        C1.viewStudentsChemistry();
                        repeatMenu = true;
                        break;
                    case 4:
                        C1.viewStudentsScience();
                        repeatMenu = true;
                        break;
                    case 5:
                        C1.viewStudentsBiology();
                        repeatMenu = true;
                        break;
                    case 6:
                        Console.Write("'nAvailable classrooms: 'n");
                        Console.Write("'nSoftware Development'n");
                        Console.Write("'nChemistry'n");
                        Console.Write("'nScience'n");
                        Console.Write("'nBiology'n");
                        Console.Write("'nPress any key to continue!");
                        Console.ReadKey();
                        Console.Clear();
                        repeatMenu = true;
                        break;
                    case 7:
                        M1.studentListMarks();
                        repeatMenu = true;
                        break;
                    case 8:
                        R1.updateMark();
                        repeatMenu = true;
                        break;
                    case 9:
                        R1.viewStudentMark();
                        repeatMenu = true;
                        break;
                    default:
                        Console.WriteLine("'nThank you for using this application, goodbye!'n");
                        break;
                }
            } while (repeatMenu == true);
        }
    }
}

迭代列表中的所有元素

正如评论中所指出的,您的问题很可能与列表中有两次相同的条目有关。但我鼓励您对列表本身进行迭代,而不是将其视为数组。

代替:

for (int i = 0; i < registerList.Count; i++)
{
    Console.WriteLine("'nStudent name: " + registerList[i].ToString() + "'nCurent student marks" + registerList[i].Mark);
}

只需这样做:

foreach(var student in registerList)
{
     Console.WriteLine("'nStudent name: " + student.ToString() + "'nCurent student marks" + student.Mark);
}

据我所见,Student只有一个实例,因此Registration的每个实例都指向同一个Student。