如何允许用户修改代码中的列表?

时间:2015-10-26 22:41:44

标签: c# list visual-studio-2015

我的计算机科学课程中有一项任务,即创建一个课程列表,为它们添加变量(即名称,前缀,学分时间),将这些课程显示到cmd,然后允许用户添加或删除当然。

我已经有了课程列表,但是我无法编写正确的代码以允许用户在列表中添加或删除项目。我可以从用户收集变量来添加课程,但我不知道如何实际使用该数据来创建一个。这是我的代码,如果它很乱,我很抱歉;我不得不玩很多东西尝试一下,我也很陌生。

我需要知道如何允许用户删除和/或添加课程列表。我的下面的代码不起作用,无论我输入什么内容,它都会在列表中不断迭代到显示中。

这是我为删除项目而编写的代码。它应该将用户输入保存为整数,并将其转发到if语句,该语句将索引位置与整数值对应。

        public static void Remove(int index)
        {
            List<Course> courseList = new List<Course>();
            Course c1 = new Course();
            Course c2 = new Course(2010, 3, "TCOM", "Technical Writing");
            Course c3 = new Course(2345, 3, "Math", "Discrete Mathematics");
            Course c4 = new Course(2332, 3, "Math", "Probablility and Data Analysis");

            courseList.Add(c1);
            courseList.Add(c2);
            courseList.Add(c3);
            courseList.Add(c4);

            courseList.RemoveAt(index);
        }

此代码块应该添加一个课程。它适当地要求用户输入变量并存储它们,但我不知道如何应用它们并将其添加到列表中。

        public static void Add()
        {
            List<Course> courseList = new List<Course>();
            Course c1 = new Course();
            Course c2 = new Course(2010, 3, "TCOM", "Technical Writing");
            Course c3 = new Course(2345, 3, "Math", "Discrete Mathematics");
            Course c4 = new Course(2332, 3, "Math", "Probability and Data Analysis");

            courseList.Add(c1);
            courseList.Add(c2);
            courseList.Add(c3);
            courseList.Add(c4);
        }

        private static void ProcessChoice(int choice)
        {
            if (choice == 1)
            {
                Console.WriteLine("Choose a course to delete, with the first course being 0.");

                if (choice == 0)
                {
                    Remove(0);
                }

                if (choice == 1)
                {
                    Remove(1);
                }

                if (choice == 2)
                {
                    Remove(2);
                }

                if (choice == 3)
                {
                    Remove(3);
                }

                if (choice == 4)
                {
                    Remove(4);
                }

                if (choice == 5)
                {
                    Remove(5);
                }

            }

            if (choice == 2)
            {
                Console.Write("What is the course number? ");
                int coursenum = Int32.Parse(Console.ReadLine());

                Console.Write("What is the prefix of your course? ");
                string pref = Console.ReadLine();

                Console.Write("What is the name of your course? ");
                string name = Console.ReadLine();

                Console.Write("How many credit hours is the course worth? ");
                int crehours = Int32.Parse(Console.ReadLine());

                List<Course> courseList = new List<Course>();
                Course c5 = new Course(coursenum, crehours, pref, name);
                courseList.Add(c5);

                Console.WriteLine(); // Once again to add space.
            }

1 个答案:

答案 0 :(得分:0)

David Kron 一样,您的方法无法访问List。使用field存储List的内容,例如:

public class X
{
    private List<Course> courseList = new List<Course>();
}

这将创建一个List<Course>的新实例,您可以在类的实例中访问该实例。您可以使用Add()将项目添加到列表中,并使用Remove()删除项目。

ProcessChoice()中的if语句是不必要的。您只需将用户的输入与列表中的数据进行比较即可。您可以使用foreach迭代列表。