2011-04-06 35 views
0

我遇到了一些麻煩,如下所示:當我添加一個新用戶,它工作正常,但當我添加第二個用戶,它忘記了第一個用戶。它會一直忘記之前添加的用戶。下面是代碼:做一個數據庫,其中用戶輸入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; 
    } 

} 

}

+0

我不確定數據庫標記在這裏有幫助。你正在記憶中的一切,不是嗎?使用數據庫標籤可以幫助那些能夠回答這些問題的人,但我不明白這是怎麼回事。 – MJB 2011-04-06 17:27:42

回答

2

目前只創建一個學生,然後覆蓋他的財產

移動這樣的:

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()等 - 這將使您的代碼更清晰,更易於閱讀。

+0

ohh ok thx,我剛剛開始c#3個月前,我16歲:) – owenattard 2011-04-06 17:47:05

+0

@owenattard:很好,保持它! – BrokenGlass 2011-04-06 17:52:23

+0

thx很多爲您的幫助 – owenattard 2011-04-06 17:59:30