做一种数据库,用户输入 id 名称等,然后当用户输入 id 时,他可以向该用户添加课程
我遇到了一些麻烦,如下:当我添加新用户时,它工作正常,但是当我添加第二个用户时,它会忘记第一个用户。它总是忘记之前添加的前一个用户。代码如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前只创建一个学生,然后覆盖他的属性
将其移动
到 do 循环中,您实际上在其中创建了一个新学生:
更好的是只有在准备好他的所有属性后才创建 Student 对象:
除此之外,您的代码看起来对我来说就像石器时代和飞出个未来的组合:一方面使用带有单独索引变量的古老循环结构,另一方面使用泛型和 LINQ。尝试尽可能坚持更高的抽象 - 如果您使用
ForEach()
等,则可以避免您使用的大多数循环变量 - 这将使您的代码更加干净且易于阅读。You are currently only creating one student, then overwrite his properties
Move this:
into your do loop where you actually create a new student:
Even better would be only creating the Student object once you have all of his properties ready:
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.