动态更改顶部边界线C#

本文关键字:边界线 顶部 动态 | 更新日期: 2023-09-27 18:24:13

所以我正在为我的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,然后计算等于总表宽度的Console.Write("_")。提前感谢您的帮助:)

动态更改顶部边界线C#

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

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

您基本上想要将宽度乘以给定的填充(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();
    }

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

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