我有一个表的四个列的四个列表。我要打印数据,如下图的屏幕快照所示。

但是,每当我运行下面的代码时,第一个用户的详细信息就会根据屏幕截图显示,但是在我输入其他用户的数据并尝试打印后,它会出现在同一行中,如下所示:

Jack Li Tom smith 10 5

这是我尝试过的:

 public void SummaryTable()
        {

            Console.WriteLine("\tSummary of Membership Fee");
            Console.WriteLine("{0} {1,10} {2,10} {3,10}","Name", "Weeks","Discount","Charges");
            //Console.Write("Weeks\t");
            //Console.Write("Discount\t");
           // Console.WriteLine("Charge");
            //Console.WriteLine();

            foreach (string namesl in nameList)
            {
                Console.Write("{0}",namesl);
                //Console.Write("\t");
            }

            foreach (int weekl in weekList)
            {
                Console.Write("{0,10}",weekl);
                //Console.Write("\t");
            }

            foreach (string discountl in discountList)
            {
                Console.Write("{0,10}", discountl);
               // Console.Write("\t");
            }

            foreach (double chargel in chargeList)
            {
                Console.WriteLine("{0,10}",chargel);



            }


c# - 如何为用户的每个新值将列表项格式化到表的不同行中-LMLPHP

最佳答案

您可以像这样打印它们:

Console.WriteLine("\tSummary of Membership Fee");
Console.WriteLine(new String('=', 45));
Console.WriteLine("{0,-20} {1,-5} {2,-10} {3,-10}", "Name", "Weeks", "Discount", "Charges");
Console.WriteLine(new String('-', 45));

for (int count = 0; count < nameList.Count; count++)
{
    Console.WriteLine("{0,-20}{1,5}{2,10}{3,10}", nameList[count], weekList[count], discountList[count], chargeList[count]);
    Console.WriteLine(new String('-', 45));
}

10-08 10:56