0

所以我正在为我的 C# 类构建一个乘法表。我已经完成了表格的代码,并且它可以像宣传的那样工作。问题是我需要一个动态变化的上边框,因为表格与用户输入的宽度数字一样宽,间距为 5 个字符。有什么想法吗?

    static void Main(string[] args)
    {
        int width, height;
        //int tableWidth;

        Console.Write("How wide do we want the multiplication table? ");
        width = Convert.ToInt32(Console.ReadLine());

        Console.Write("How high do we want the multiplication table? ");
        height = Convert.ToInt32(Console.ReadLine());

        Console.Write("    x|");

        for (int x = 1; x <= width; x++)
            Console.Write("{0, 5}", x);


            Console.WriteLine();




        for (int row = 1; row <= height; row++)
        {
            Console.Write("{0, 5}|", row);
            for (int column = 1; column <= height; ++column)
            {
                Console.Write("{0, 5}", row * column);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }

我假设tableWidth需要计算,然后aConsole.Write("_")等于总表的宽度。在此先感谢您的帮助 :)

4

3 回答 3

0

您基本上想将宽度乘以您给出的填充(5)。像这样的东西会起作用。请记住,我认为您应该将一些间距分解为变量,因为您在许多地方重复使用像 5 这样的常量值。此外,x 和管道之前的间距可能应该是一个字符串变量,否则很难看出每个空格有多少。这是我的解决方案,使代码保持与迄今为止相同的格式:

    Console.Write("    x|");

    for (int x = 1; x <= width; x++)
        Console.Write("{0, 5}", x);

    Console.WriteLine();

    Console.Write("     |");

    for (int x = 1; x <= (width * 5); x++)
        Console.Write("-");


    Console.WriteLine();

制作整个方法:

    static void Main(string[] args)
    {
        int width, height;
        //int tableWidth;

        Console.Write("How wide do we want the multiplication table? ");
        width = Convert.ToInt32(Console.ReadLine());

        Console.Write("How high do we want the multiplication table? ");
        height = Convert.ToInt32(Console.ReadLine());

        Console.Write("    x|");

        for (int x = 1; x <= width; x++)
            Console.Write("{0, 5}", x);

        Console.WriteLine();

        Console.Write("     |");

        for (int x = 1; x <= (width * 5); x++)
            Console.Write("-");

        Console.WriteLine();

        for (int row = 1; row <= height; row++)
        {
            Console.Write("{0, 5}|", row);
            for (int column = 1; column <= height; ++column)
            {
                Console.Write("{0, 5}", row * column);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
于 2013-07-21T21:45:43.443 回答
0

这是您的代码,具有改进的命名和顶线绘制(顺便说一句,您显示的行不正确 - 您在第二个循环中迭代行而不是列):

const int columnWidth = 5;
string cellFormat = "{0, " + columnWidth + "}"; 

Console.Write("How wide do we want the multiplication table? ");
int columnsCount = Convert.ToInt32(Console.ReadLine());

Console.Write("How high do we want the multiplication table? ");
int rowsCount = Convert.ToInt32(Console.ReadLine());           

string title = String.Format(cellFormat + "|", "x");
Console.Write(title);

for (int i = 1; i <= columnsCount; i++)
    Console.Write(cellFormat, i);

Console.WriteLine();
int tableWidth = columnWidth * columnsCount + title.Length;
Console.WriteLine(new String('-', tableWidth));

for (int row = 1; row <= rowsCount; row++)
{
    Console.Write(cellFormat + "|", row);

    for (int column = 1; column <= columnsCount; column++)
        Console.Write(cellFormat, row * column);

    Console.WriteLine();
}

下一个重构步骤是提取类和方法:

Console.Write("How wide do we want the multiplication table? ");
int columnsCount = Convert.ToInt32(Console.ReadLine());

Console.Write("How high do we want the multiplication table? ");
int rowsCount = Convert.ToInt32(Console.ReadLine());

MultiplicationTable table = new MultiplicationTable(columnsCount, rowsCount);
table.Draw();

现在代码更清晰了——它告诉你有乘法表,你想画它。绘图很简单 - 您绘制列标题和原始数据:

public class MultiplicationTable
{
    private const int columnWidth = 5;
    private string cellFormat = "{0, " + columnWidth + "}";
    private int columnsCount;
    private int rowsCount;      

    public MultiplicationTable(int columnsCount, int rowsCount)
    {
        this.columnsCount = columnsCount;
        this.rowsCount = rowsCount;
    }

    public void Draw()
    {            
        DrawColumnHeaders();
        DrawRaws();
    }

    private void DrawColumnHeaders()
    {
        string title = String.Format(cellFormat + "|", "x");
        Console.Write(title);

        for (int i = 1; i <= columnsCount; i++)
            Console.Write(cellFormat, i);

        Console.WriteLine();
        int tableWidth = columnWidth * columnsCount + title.Length;
        Console.WriteLine(new String('-', tableWidth));
    }

    private void DrawRaws()
    {
        for (int rowIndex = 1; rowIndex <= rowsCount; rowIndex++)
            DrawRaw(rowIndex);
    }

    private void DrawRaw(int rowIndex)
    {
        DrawRawHeader(rowIndex);

        for (int columnIndex = 1; columnIndex <= columnsCount; columnIndex++)
            DrawCell(rowIndex * columnIndex);

        Console.WriteLine();
    }

    private void DrawRawHeader(int rowIndex)
    {
        Console.Write(cellFormat + "|", rowIndex);
    }

    private void DrawCell(int value)
    {
        Console.Write(cellFormat, value);
    }
}
于 2013-07-21T21:47:48.367 回答
0

您需要使用另一个循环,如下所示:

Console.Write("How wide do we want the multiplication table? ");
int width = Convert.ToInt32(Console.ReadLine());
Console.Write("How high do we want the multiplication table? ");
int height = Convert.ToInt32(Console.ReadLine());
Console.Write("    ");
for (int i = 0; i < width; i++) 
    Console.Write("_____");
Console.WriteLine("__");
Console.Write("    x|");
for (int x = 1; x <= width; x++)
    Console.Write("{0, 5}", x);
Console.WriteLine();
for (int row = 1; row <= height; row++)
{
    Console.Write("{0, 5}|", row);
    for (int column = 1; column <= height; ++column)
    {
        Console.Write("{0, 5}", row * column);
    }
    Console.WriteLine();
}
Console.ReadLine();
于 2013-07-21T21:49:23.233 回答