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計算する必要がConsole.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

これは、名前付けと上部の線画が改善されたコードです(ところで、間違った行が表示されています-2番目のループで列ではなく行を繰り返しています):

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

これでコードはより明確になりました。掛け算の表があり、それを描画する必要があることがわかります。描画は簡単です - 列ヘッダーと raw を描画します:

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 に答える