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

标签 c#

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

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

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);



            }

enter image description here

最佳答案

你可以像这样打印它们:

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));
}

关于c# - 如何为用户的每个新值将列表项格式化为不同的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069401/

相关文章:

c# - DotNet HighCharts ,用查询结果填充饼图

c# - 将 Access 报告导出为 PDF

c# - 即使进行简约测试,GCHandle.FromIntPtr 也始终显示 "value belongs to another domain"

c# - 如何开始制作 C# RSS 阅读器?

c# - Xamarin.Forms 分组 ListView 无法在 Android 中添加新组

c# - 将事件处理程序附加到动态 com 对象

c# - NPOI 设置不同的单元格格式

c# - 秒表时间始终为零

javascript - 如果数字为 1 或 0,则显示不同的图标 - ASP.NET C# MVC JQuery

c# - 通过 javascript 调用 C# windows 服务函数