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



我试图创建一个工作人员输入客户id和显示客户信息的银行场景,我遇到了类不可访问的问题,现在它似乎根本找不到它。

public class Program
{
public class customer1
{
string name = "Akan Udoh";
int id = 101;
String type = "Current";
}
public class customer2
{
string name = "Clara Udoh";
int id = 102;
string type = "Savings";
}
public class customer3
{
string name = "jane doe";
int id = 103;
string type = "Fixed deposit";
}
static void Main(string[] args)
{
Console.WriteLine("Enter Customer id");
int id = Convert.ToInt32(Console.ReadLine());
switch (id)
{
case 101:
customer1 customer1 = new customer1();
Console.WriteLine(customer1);
Console.WriteLine(customer1);
Console.WriteLine(customer1);
break;
case 102:
customer2 customer2 = new customer2();
Console.WriteLine(customer2);
Console.WriteLine(customer2);
Console.WriteLine(customer2);
break;
case 103:
customer3 customer3 = new customer3();
Console.WriteLine(customer3);
Console.WriteLine(customer3);
Console.WriteLine(customer3);
}
}
}

欢迎来到StackOverflow!

正如其他人在评论中提到的,您可能不希望为每个客户创建一个类,而是希望创建一个具有多个实例的单个customer类。

我相信你想达到这样的目标:

public class Program
{
public class Customer
{
public string Name;
public int Id;
public string Type;
}
static void Main(string[] args)
{
var customers = new Customer[]
{
new Customer
{
Name = "Akan Udoh",
Id = 101,
Type = "Current"
},
new Customer
{
Name = "Clara Udoh",
Id = 102,
Type = "Savings"
},
new Customer
{
Name = "jane doe",
Id = 103,
Type = "Fixed deposit"
},
};
// As an alternative, you could add all customers to a dictionary for a faster search
// var customerDictionary = new Dictionary<int, Customer>();
// foreach (Customer cust in customers)
//     customerDictionary.Add(cust.Id, cust);
Console.WriteLine("Enter Customer id");
var id = Convert.ToInt32(Console.ReadLine());
var customer = customers.Where(x => x.Id == id).FirstOrDefault();
// If you choose a dictionary, you can find it like this instead of the above Linq query
// var customer = customerDictionary[id];
Console.WriteLine(customer.Name);
Console.WriteLine(customer.Id);
Console.WriteLine(customer.Type);
}
}

相关内容

  • 没有找到相关文章

最新更新