2015-11-14 164 views
0

我有一個用戶類,我想用它來創建用戶列表,直到用戶不再需要爲止。我不知道如何從控制檯獲取輸入。我已經刪除了我的主代碼,因爲它是一個混亂的混亂,並且寧願從頭開始。我的用戶類如下。通過用戶輸入將用戶添加到列表中C#

class Users 
{ 
    List<Users> _userList = new List<Users>(); 

    private string _name; 
    private int _age; 
    private string _address; 
    private string _phone; 

    public Users(string name, int age, string address, string phone) 
    { 
     _name = name; 
     _age = age; 
     _address = address; 
     _phone = phone; 
    } 

    public string GetName() 
    { 
     return _name; 
    } 

    public void SetName(string name) 
    { 
     _name = name; 
    } 

    public int GetAge() 
    { 
     return _age; 
    } 

    public void SetAge(int age) 
    { 
     _age = age; 
    } 

    public string GetAddress() 
    { 
     return _address; 
    } 

    public void SetAddress(string address) 
    { 
     _address = address; 
    } 

    public string GetPhone() 
    { 
     return _phone; 
    } 

    public void SetPhone(string phone) 
    { 
     _phone = phone; 
    } 

} 

乾杯從用戶類

+0

控制檯應用程序? –

+0

什麼樣的用戶輸入?安慰 ?桌面 ? – kirotab

+0

對不起,是的控制檯@saranshkataria – user5561715

回答

1

先刪除列表,並將其重命名爲用戶。

public class User 
{ 
    private string _name; 
    private int _age; 
    private string _address; 
    private string _phone; 

    public User(string name, int age, string address, string phone) 
    { 
     _name = name; 
     _age = age; 
     _address = address; 
     _phone = phone; 
    } 

    //... 
} 

然後在控制檯程序類中聲明用戶類列表,並將新用戶添加到列表中。根據用戶控制檯輸入設置用戶屬性。

List<User> _userList = new List<User>(); 

static void Main(string[] args) 
{ 
    Console.Write("Name: "); 
    string name = Console.ReadLine(); 

    Console.Write("Age: "); 
    int age = int.Parse(Console.ReadLine()); 

    Console.Write("Address: "); 
    string address = Console.ReadLine(); 

    Console.Write("Phone: "); 
    string phone = Console.ReadLine(); 

    User user = new User(name, age, address, phone); 
    _userList.Add(user); 
} 
+0

感謝您的回答,但我試圖將其作爲控制檯應用程序而不是Windows窗體來完成 – user5561715

1

首先,需要注意的是List<Users> _userList = new List<Users>();在類不需要的。你沒有在任何地方使用它。一個List<T>結構是存儲多個用戶的好方法 - 只需用代表用戶的類型替換T即可。您應該更改班級的名稱以代表單個用戶(User在這裏是個好主意),並在班級以外使用List<User>

看看這個人爲的例子,其中用戶有一個string屬性 - 用戶的名稱。它使您可以將多個用戶添加到您選擇的名稱列表中,然後以新行打印每個名稱。請注意,我使用自動執行的屬性來存儲用戶的名稱。

class User 
{ 
    public User(string name) 
    { 
     Name = name; 
    } 

    public Name { get; private set; } 
} 

public static void Main() 
{ 
    List<User> users = new List<User>(); 
    bool anotherUser = true; 
    while (anotherUser) 
    { 
     Console.WriteLine("Please specify a name."); 
     string userName = Console.ReadLine(); 
     User user = new User(userName); 
     users.Add(user); 
     string next = Console.WriteLine("Do you want to add another user (type Y for yes)?"); 
     anotherUser = (next == "Y"); 
    } 

    Console.WriteLine("\nNames of added users:"); 
    foreach(User u in users) 
    { 
     Console.WriteLine(u.Name); 
    } 

    Console.ReadKey(); 
}  

當然,你必須展開這個答案才能真正得到你想要的。這僅僅是一個參考點。

0

讓我們考慮一個簡單的例子來獲得一個基本的瞭解:

正如其他人已經建議可以提高User類如下圖所示,C#有Auto-Implemented Properties一個概念,編譯器將處理的getter/setter代碼生成幕後爲你,所以至少你的代碼是足夠乾淨的!再次有時你可能需要有構造函數注入屬性值或顯式方法來設置值,我不會去。

public class User 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 

    //Other properties/indexers/delegates/events/methods follow here as required. Just find what all these members are in C#!! 
} 

代碼接受用戶輸入:

static void Main(string[] args) 
{ 
    List<User> users = new List<User>(); 

    char createAnotherUser = 'N'; 

    do 
    { 
      var user = new User(); 
      int age; 

      Console.Write("\nUser Name: "); 
      user.Name = Console.ReadLine(); 

      Console.Write("Age: "); 
      string ageInputString = Console.ReadLine(); 

      //Validate the provided age is Int32 type. If conversion from string to Int32 fails prompt user until you get valid age. 
      //You can refactor and extract to separate method for validation and retries etc., as you move forward. 
      while (!int.TryParse(ageInputString, out age)) //Notice passing parameter by reference with 'out' keyword and it will give us back age as integer if the parsing is success. 
      { 
       Console.Write("Enter a valid Age: "); 
       ageInputString = Console.ReadLine(); 
      } 

      //Accept other data you need and validate if required 

      users.Add(user); //Add the user to the List<User> defined above 

      //Confirm if another user to be created 
      Console.Write("Do you want to create another User[Y/N]? : "); 
      createAnotherUser = char.ToUpper(Console.ReadKey(false).KeyChar); //Compare always upper case input irrespective of user input casing. 

    } while (createAnotherUser == 'Y'); 

您可以瞭解更多有關使用out keyword in MSDN

希望這提供你一些想法引用傳遞變量...

相關問題