迭代列表中的所有元素

时间:2014-04-23 14:32:53

标签: c#

过去两周我一直在做作业,我很难克服这个问题。

以下是代码。这绝不是完成的代码,无论如何,一旦我将所有部件都工作,我将整理它。在主要案例1中:在询问了一系列问题后,我想将S1传递给R1.addStudent方法,这是有效的,但是,如果你查看注册类,addStudent方法会添加S1对象/数据成员和标记为列表。

我的问题是,一旦我尝试遍历此列表(请参阅模块类),它会迭代元素的数量但返回最后一个条目名称两次(使用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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如评论中所述,您的问题很可能与列表中的相同条目有两次相关。但是我想鼓励你遍历列表本身,而不是像对待数组那样对待它。

而不是:

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);
}

答案 1 :(得分:1)

据我所知,只有一个学生实例,因此每个注册实例都指向同一个学生。