做一种数据库,用户输入 id 名称等,然后当用户输入 id 时,他可以向该用户添加课程

发布于 2024-10-30 23:24:35 字数 4256 浏览 1 评论 0原文

我遇到了一些麻烦,如下:当我添加新用户时,它工作正常,但是当我添加第二个用户时,它会忘记第一个用户。它总是忘记之前添加的前一个用户。代码如下:

    static List<Students> stud = new List<Students>();
    static Courses regcor = new Courses(0,"");
    static void Main(string[] args)
    {
        Students regstud = new Students("", "", "", 0);
        string idselec = "";
        string selec = "";
        do
        {


            Console.WriteLine("1.Register new student.");
            Console.WriteLine("2.Add course.");
            Console.WriteLine("3.All information.");
            Console.WriteLine("4.Exit.");
            selec = Console.ReadLine();
            if (selec == "1")
            {
                do
                {
                    Console.Clear();
                    Console.WriteLine("Enter ID");
                    idselec = Console.ReadLine();
                    if (checkid(idselec))
                    {
                        Console.WriteLine("Id already excsists");
                        Console.ReadLine();
                    }

                }
                while (checkid(idselec));
                regstud.ID = idselec;
                Console.WriteLine("Enter Name");
                regstud.name = Console.ReadLine();
                Console.WriteLine("Enter Surname");
                regstud.surname = Console.ReadLine();
                Console.WriteLine("Enter Age");
                regstud.age = Convert.ToInt32(Console.ReadLine());
                stud.Add(regstud);
            }
            else if (selec == "2")
            {
                Console.WriteLine("Enter ID");
                idselec = Console.ReadLine();
                check(idselec);

            }
            else if (selec == "3") 
            {
                Console.Clear();
                writeall();
            }

        }
        while (selec != "4");


    }
    static bool checkid(string id)
    {

        return stud.Any(u => u.ID == id);
    }

    static void check(string ID)
    {
        int i = 0;
        bool found = false;
        do
        {
            if (stud[i].ID == ID )
            {

                Console.WriteLine("Hello " + stud[i].name+" "+ stud[i].surname) ;
                Console.WriteLine("Enter code");
                int code =Convert.ToInt32( Console.ReadLine());
                Console.WriteLine("Enter Name");
                string name = Console.ReadLine();

                regcor.CourID = code;
                regcor.courname = name;
                stud[i].cour(regcor);

                found = true;


            }

            i++;
        }
        while ((i < stud.Count) && !(found));




    }
    static void writeall()
    {
        int i = 0, y=0, sub=0,sub3=0;
        string sub2="";
        do
        {
            Console.WriteLine(stud[i].ID);
            Console.WriteLine(stud[i].name);
            Console.WriteLine(stud[i].surname);
            Console.WriteLine(stud[i].age);
            sub3 = stud[i].cour3();
            do
            {
                sub = stud[i].cour1(y);
                sub2 = stud[i].cour2(y);
                Console.WriteLine(sub);
                Console.WriteLine(sub2);

                y++;


            }
            while (y < sub3);
            i++;
        }
        while (i < stud.Count);
    }
}

}

这是课程 Courses:

    public int CourID = 0;
    public string courname = "";
    public Courses(int corID, string corname)
    {
        this.CourID = corID;
        this.courname = corname;

    }
}

}

这是学生课程;

    public int age = 0;
    public string name = "", surname = "", ID = "";
    List<Courses> cours = new List<Courses>();
    public Students(string name, string surname,string ID, int age)
    {
        this.ID = ID;
        this.surname = surname;
        this.age = age;
        this.name = name;
    }
    public void cour(Courses c)
    {
        cours.Add(c);
    }
    public int cour1(int i)
    {
       return cours[i].CourID;

    }
    public string cour2(int i)
    {
      return  cours[i].courname;
    }
    public int cour3()
    {
        return cours.Count;
    }

}

}

I am having some trouble as follows: When i add a new user, it works fine but when I add the second user, it forgets the first one. It keeps forgetting the preceding user added before it. Here is the code:

    static List<Students> stud = new List<Students>();
    static Courses regcor = new Courses(0,"");
    static void Main(string[] args)
    {
        Students regstud = new Students("", "", "", 0);
        string idselec = "";
        string selec = "";
        do
        {


            Console.WriteLine("1.Register new student.");
            Console.WriteLine("2.Add course.");
            Console.WriteLine("3.All information.");
            Console.WriteLine("4.Exit.");
            selec = Console.ReadLine();
            if (selec == "1")
            {
                do
                {
                    Console.Clear();
                    Console.WriteLine("Enter ID");
                    idselec = Console.ReadLine();
                    if (checkid(idselec))
                    {
                        Console.WriteLine("Id already excsists");
                        Console.ReadLine();
                    }

                }
                while (checkid(idselec));
                regstud.ID = idselec;
                Console.WriteLine("Enter Name");
                regstud.name = Console.ReadLine();
                Console.WriteLine("Enter Surname");
                regstud.surname = Console.ReadLine();
                Console.WriteLine("Enter Age");
                regstud.age = Convert.ToInt32(Console.ReadLine());
                stud.Add(regstud);
            }
            else if (selec == "2")
            {
                Console.WriteLine("Enter ID");
                idselec = Console.ReadLine();
                check(idselec);

            }
            else if (selec == "3") 
            {
                Console.Clear();
                writeall();
            }

        }
        while (selec != "4");


    }
    static bool checkid(string id)
    {

        return stud.Any(u => u.ID == id);
    }

    static void check(string ID)
    {
        int i = 0;
        bool found = false;
        do
        {
            if (stud[i].ID == ID )
            {

                Console.WriteLine("Hello " + stud[i].name+" "+ stud[i].surname) ;
                Console.WriteLine("Enter code");
                int code =Convert.ToInt32( Console.ReadLine());
                Console.WriteLine("Enter Name");
                string name = Console.ReadLine();

                regcor.CourID = code;
                regcor.courname = name;
                stud[i].cour(regcor);

                found = true;


            }

            i++;
        }
        while ((i < stud.Count) && !(found));




    }
    static void writeall()
    {
        int i = 0, y=0, sub=0,sub3=0;
        string sub2="";
        do
        {
            Console.WriteLine(stud[i].ID);
            Console.WriteLine(stud[i].name);
            Console.WriteLine(stud[i].surname);
            Console.WriteLine(stud[i].age);
            sub3 = stud[i].cour3();
            do
            {
                sub = stud[i].cour1(y);
                sub2 = stud[i].cour2(y);
                Console.WriteLine(sub);
                Console.WriteLine(sub2);

                y++;


            }
            while (y < sub3);
            i++;
        }
        while (i < stud.Count);
    }
}

}

This is the class Courses:

    public int CourID = 0;
    public string courname = "";
    public Courses(int corID, string corname)
    {
        this.CourID = corID;
        this.courname = corname;

    }
}

}

This is the class Students;

    public int age = 0;
    public string name = "", surname = "", ID = "";
    List<Courses> cours = new List<Courses>();
    public Students(string name, string surname,string ID, int age)
    {
        this.ID = ID;
        this.surname = surname;
        this.age = age;
        this.name = name;
    }
    public void cour(Courses c)
    {
        cours.Add(c);
    }
    public int cour1(int i)
    {
       return cours[i].CourID;

    }
    public string cour2(int i)
    {
      return  cours[i].courname;
    }
    public int cour3()
    {
        return cours.Count;
    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-11-06 23:24:35

您当前只创建一个学生,然后覆盖他的属性

将其移动

Students regstud = new Students("", "", "", 0);

到 do 循环中,您实际上在其中创建了一个新学生:

if (selec == "1")
{
   Students regstud = new Students("", "", "", 0);
   ...
}

更好的是只有在准备好他的所有属性后才创建 Student 对象:

if (selec == "1")
{
   ...
   Console.WriteLine("Enter Name");
   string name = Console.ReadLine();
   Console.WriteLine("Enter Surname");
   string surname = Console.ReadLine();
   Console.WriteLine("Enter Age");
   int age = Convert.ToInt32(Console.ReadLine());

   Students regstud = new Students(name, surename, id, age);
   ...
}

除此之外,您的代码看起来对我来说就像石器时代和飞出个未来的组合:一方面使用带有单独索引变量的古老循环结构,另一方面使用泛型和 LINQ。尝试尽可能坚持更高的抽象 - 如果您使用 ForEach() 等,则可以避免您使用的大多数循环变量 - 这将使您的代码更加干净且易于阅读。

You are currently only creating one student, then overwrite his properties

Move this:

Students regstud = new Students("", "", "", 0);

into your do loop where you actually create a new student:

if (selec == "1")
{
   Students regstud = new Students("", "", "", 0);
   ...
}

Even better would be only creating the Student object once you have all of his properties ready:

if (selec == "1")
{
   ...
   Console.WriteLine("Enter Name");
   string name = Console.ReadLine();
   Console.WriteLine("Enter Surname");
   string surname = Console.ReadLine();
   Console.WriteLine("Enter Age");
   int age = Convert.ToInt32(Console.ReadLine());

   Students regstud = new Students(name, surename, id, age);
   ...
}

Besides this your code looks like of a combination of Stone Age and Futurama to me: You use archaic loop constructs with separate index variables on the one hand, and then generics and LINQ on the other. Try to stick with higher abstractions as possible - most loop variables you use can be avoided, if you use ForEach() etc - this will make your code much cleaner and easier to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文