为什么控制台窗口不垂直而不是水平打印阵列内容?有办法改变吗?如何使用Console.WriteLine()水平而不是垂直显示数组的内容?
C#
ArrayList tryArray = new ArrayList();
for (int i = 1; i <= 2; i++)
{
// ASKING FOR THE USER TO ENTER DETAILS
Console.WriteLine("Please enter information for customer: " + i);
Console.WriteLine("Please enter your customer name");
cusName = Console.ReadLine();
tryArray.Add(cusName);
Console.WriteLine("Please enter your customer id");
cusID = Console.ReadLine();
tryArray.Add(cusID);
Console.WriteLine("No of months for membership ? (Range 1-120)");
memMonths = Convert.ToInt32(Console.ReadLine());
tryArray.Add(memMonths.ToString());
}
Console.WriteLine("{0,-20} {1,10} {2,10}}", "Customer Name","Customer ID", "Months");
for (int j = 0; j < tryArray.Count; j++)
{
Console.WriteLine("{0,-10} {1,5} {2,5}", tryArray[j], tryArray[j], tryArray[j]);
}
**//code print like this right now
Customer Name Customer ID Months
a1 a1 a1
a1234567 a1234567 a1234567
12 12 12
a2 a2 a2
a2234567 a2234567 a2234567
15 15 15
//Result I want
Customer Name Customer ID Months
a1 a1234567 12
a2 a2234567 15**
我认为您应该使用这样的列表和类:
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer("a1", "a1234567", 12));
customers.Add(new Customer("a2", "a2234567", 15));
foreach (Customer c in customers)
{
Console.WriteLine(c.Name +" "+ c.Id+" "+ c.Months);
}
Console.ReadKey();
}
class Customer
{
public string Name { get; set; }
public string Id { get; set; }
public int Months { get; set; }
public Customer(string name, string id, int months)
{
Name = name;
Id = id;
Months = months;
}
}
输出:
a1 a1234567 12
a2 a2234567 15