我遇到了一些麻煩,如下所示:當我添加一個新用戶,它工作正常,但當我添加第二個用戶,它忘記了第一個用戶。它會一直忘記之前添加的用戶。下面是代碼:做一個數據庫,其中用戶輸入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);
}
}
}
這是類課程:
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;
}
}
}
我不確定數據庫標記在這裏有幫助。你正在記憶中的一切,不是嗎?使用數據庫標籤可以幫助那些能夠回答這些問題的人,但我不明白這是怎麼回事。 – MJB 2011-04-06 17:27:42