C#使用循环生成具有正确算术关系的表

时间:2015-11-02 02:17:27

标签: c# loops tabular

以下是ShowTab()方法,如何将动态数字和结果应用于表格?

using System;


const int MAX = 4;
int cage = 500/total;

int month = 1; 
int adults = 1; 
int babies = 0;
int total  = 1;

Console.WriteLine("Month\tAdults\tBabies\tTotal");
Console.WriteLine("{0, -10}{1, -10}{2, -10}{3, -10}", month, adults, babies, total);

for(int i = 0; i < MAX; i++) {
Console.writeLine(  
}

1 个答案:

答案 0 :(得分:1)

也许我错过了什么;但如果它仅仅是关于格式化的某种方式;这样的事情应该做的工作:

int month = 1;
int adults = 1;
int babies = 0;
int total = 1;

Console.WriteLine ("header row"); // optional (if needed)

while (/* there is still cages to hold them */)
{
    // print current state (-10 width chosen for example, negative for left align)
    Console.WriteLine ($"{month, -10}{adults, -10}{babies, -10}{total, -10}");

    // do the maths to update values
    month = /* ... */;
    adults = /* ... */;
    babies = /* ... */;
    total = /* ... */;
}

这是一个虚拟exemple,它说明了为什么我选择使用宽度格式说明符而不是制表符(如在一条评论链接中暗示的那样)。