2013-07-21 83 views
0

所以我正在爲我的C#類構建一個乘法表。我已經完成了表格的代碼,並且它按照廣告的方式工作。問題是我需要一個動態變化的頂部邊框,因爲表格寬度與用戶爲寬度數字輸入的數字一樣寬,間距爲5個字符。有什麼想法嗎?動態更改頂部邊框線C#

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

你基本上想要乘以你給的填充(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(); 
    } 
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(); 
} 

的Nex t重構步驟是提取類和方法:

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

感謝。這肯定有幫助。它並沒有完全得到我想要的,但是在第一個for語句中,我添加了〜for(int i = 1; i <= width + 1; i ++);〜並且得到了它。再次感謝!!! – user1707042