.net 我尝试使用开关大小写来显示存储在类中的数据

arknldoa  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(134)

我试图创建一个银行场景,其中工人输入客户ID和客户的信息显示,我已经与类无法访问的问题,现在它似乎无法找到它在所有。

  1. public class Program
  2. {
  3. public class customer1
  4. {
  5. string name = "Akan Udoh";
  6. int id = 101;
  7. String type = "Current";
  8. }
  9. public class customer2
  10. {
  11. string name = "Clara Udoh";
  12. int id = 102;
  13. string type = "Savings";
  14. }
  15. public class customer3
  16. {
  17. string name = "jane doe";
  18. int id = 103;
  19. string type = "Fixed deposit";
  20. }
  21. static void Main(string[] args)
  22. {
  23. Console.WriteLine("Enter Customer id");
  24. int id = Convert.ToInt32(Console.ReadLine());
  25. switch (id)
  26. {
  27. case 101:
  28. customer1 customer1 = new customer1();
  29. Console.WriteLine(customer1);
  30. Console.WriteLine(customer1);
  31. Console.WriteLine(customer1);
  32. break;
  33. case 102:
  34. customer2 customer2 = new customer2();
  35. Console.WriteLine(customer2);
  36. Console.WriteLine(customer2);
  37. Console.WriteLine(customer2);
  38. break;
  39. case 103:
  40. customer3 customer3 = new customer3();
  41. Console.WriteLine(customer3);
  42. Console.WriteLine(customer3);
  43. Console.WriteLine(customer3);
  44. }
  45. }
  46. }

字符串

ssm49v7z

ssm49v7z1#

正如其他人在评论中提到的那样,您可能不想为每个客户创建一个类,而是希望创建一个具有许多示例的单个Customer类。
我相信你想实现这样的目标:

  1. public class Program
  2. {
  3. public class Customer
  4. {
  5. public string Name;
  6. public int Id;
  7. public string Type;
  8. }
  9. static void Main(string[] args)
  10. {
  11. var customers = new Customer[]
  12. {
  13. new Customer
  14. {
  15. Name = "Akan Udoh",
  16. Id = 101,
  17. Type = "Current"
  18. },
  19. new Customer
  20. {
  21. Name = "Clara Udoh",
  22. Id = 102,
  23. Type = "Savings"
  24. },
  25. new Customer
  26. {
  27. Name = "jane doe",
  28. Id = 103,
  29. Type = "Fixed deposit"
  30. },
  31. };
  32. // As an alternative, you could add all customers to a dictionary for a faster search
  33. // var customerDictionary = new Dictionary<int, Customer>();
  34. // foreach (Customer cust in customers)
  35. // customerDictionary.Add(cust.Id, cust);
  36. Console.WriteLine("Enter Customer id");
  37. var id = Convert.ToInt32(Console.ReadLine());
  38. var customer = customers.Where(x => x.Id == id).FirstOrDefault();
  39. // If you choose a dictionary, you can find it like this instead of the above Linq query
  40. // var customer = customerDictionary[id];
  41. Console.WriteLine(customer.Name);
  42. Console.WriteLine(customer.Id);
  43. Console.WriteLine(customer.Type);
  44. }
  45. }

字符串

展开查看全部

相关问题