所以我正在为我的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("_")
等于总表的宽度。在此先感谢您的帮助:)
答案 0 :(得分: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();
}
答案 1 :(得分: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);
}
}
答案 2 :(得分: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();