简体   繁体   English

将用户输入添加到类列表

[英]adding user input to a list of classes

I have a computer assignment I am stuck on.我有一个计算机作业,我被困住了。 I have to make a list of college classes, and let the user add to or delete from that list.我必须列出大学课程,然后让用户在该列表中添加或删除。 I have the basic list made, and my printmenu loop, but cannot figure out how to make the user input into another Class in classlist.我已经制作了基本列表和我的打印菜单循环,但无法弄清楚如何让用户输入到类列表中的另一个类。

Here is my main.cs thus far....到目前为止,这是我的 main.cs....

using System;
using System.Collections.Generic;

namespace Lab04
{
    class MainClass
    {
        public static string prefix;
        public static int coursenumber;
        public static int credithours;
        public static string coursedescription;


        public static void Main (string[] args) //creates 4 courses (used some leftover Lab 03 data and some new) 
        {

            Course c1 = new Course();
            Course c2 = new Course("ENGL", 1102, 3, "Second level class for English Composition.");
            Course c3 = new Course("GEOG", 1113, 4, "This is an online lab course for Geography.");
            Course c4 = new Course("BWV", 101, 2, "This is basket weaving on a college level, for those of you who need a break.");


            Console.WriteLine ("Here are your current courses for this semester:");

            List<Course> classlist = new List<Course>();
            classlist.Add (c1);
            classlist.Add (c2);
            classlist.Add (c3);
            classlist.Add (c4);

            foreach(Course c in classlist) 
                Console.WriteLine(c + " ");
            Console.WriteLine();

            int choice;
            do {
            PrintMenu();
            choice = Int32.Parse (Console.ReadLine());
            ProcessChoice(choice);
            } while (choice != 4);
            Console.WriteLine ("Thank you for using this system.");

        }

        public static void PrintMenu() //print menu for user input
        {
            Console.WriteLine("Main Menu:"); 
            Console.WriteLine("1. Add a Course to the list.");
            Console.WriteLine("2. Delete a Course from the list.");
            Console.WriteLine("3. Print your class schedule.");
            Console.WriteLine("4. Exit this System.");
            Console.WriteLine("Please enter your numerical choice.");

        }

        static void ProcessChoice(int c) // do while loop code for user input
        {
            if (c == 1)
            {
                Console.WriteLine ("Please enter the Course prefix.");
                prefix = Console.ReadLine();
                Console.WriteLine ("Please enter the Course number.");
                coursenumber = Int32.Parse(Console.ReadLine());
                Console.WriteLine ("How many credit hours is this course worth?");
                credithours = Int32.Parse(Console.ReadLine());
                Console.WriteLine ("Please enter a short course description.");
                coursedescription = Console.ReadLine();
            }

            else if (c == 2)
            {
                Console.WriteLine ("Which course would you like to remove?");
            }

            else if (c == 3)
            {
                Console.WriteLine ("Displaying current class schedule.");


            }
        }
    }
}

I looked at this post Adding multiple user input to a List c# , but since it just added integers, not a whole class to the list, it did not answer my question我看了这篇文章将多个用户输入添加到列表 c# ,但由于它只是向列表添加了整数,而不是整个类,因此它没有回答我的问题

There are multiple ways.有多种方式。 One way is to make classlist global and一种方法是使类列表全局化

public void ProcessChoice(int c)
{
    if (c == 1)
    {
        //Get the input from console
        classlist.Add(new Course{
            prefix = prefix,
            coursenumber = coursenumber,
            credithours = credithours,
            coursedescription = coursedescription 
        });
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM