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

本文关键字:用户 id 输入 添加 然后 一种 数据库 | 更新日期: 2023-09-27 17:49:18

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

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

}

这是课程:

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

}

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

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

移动:

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

放入do循环中,创建一个新的student:

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

更好的做法是只在你准备好了Student对象的所有属性后才创建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);
   ...
}

除此之外,你的代码在我看来就像石器时代和Futurama的结合:你一方面使用带有单独索引变量的古老循环结构,另一方面使用泛型和LINQ。尽量坚持更高的抽象-如果你使用ForEach()等,你可以避免使用大多数循环变量-这将使你的代码更干净,更容易阅读。