简体   繁体   中英

Dynamically changing top border line C#

So I'm building a multiplication table for my C# class. I've got the code for the table complete, and it works as advertised. The issue is that I need a dynamically changing top border, because the table is as wide as the number the user enters for the width digit, at 5 character spacing. Any thoughts?

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

I assume tableWidth needs to be calculated, and then a Console.Write("_") equal to the width of the total table. Thanks in advance for your help :)

You basically want to multiply the width by the padding you have given (5). Something like this will work. Keep in mind that I think you should break some of the spacing out into variables because you are re-using constant values like 5 in many places. Also the spacing before the x and the pipe should probably be a string variable, otherwise it is hard to see how many spaces go into each one. Here is my solution keeping the code in the same format you have so far:

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

Making the whole method:

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

Here is your code with improved naming and top line drawing (btw you have incorrect rows displaying - you are iterating over rows instead of columns in second loop):

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

Next refactoring step is extracting classes and methods:

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

Now code is more clear - it tells that you have multiplication table, and you want to draw it. Drawing is simple - you draw column headers and raws:

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

You need to use another loop, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM