2012-11-08 163 views
1

我想格式化一些輸出到控制檯,但有一些解決方案的問題。 我在C#中完成它,但是每當我調用Console.Write時,它會將整個事件打印到控制檯的最後,然後開始一個新行。所以我想要做的就是將它調整到四列,然後在那裏開始一個新的線路。C#列格式化

這裏是正確的方式輸出應該看起來像在控制檯:

Sam  John  Bob  Adam 

Kelly  Nolan  Carl Tim 

Tom  David 

這裏是什麼礦原來的樣子,但它的錯誤的方式:

Sam John Bob Adam Kelly Nolan Carl Tim 

Tom David 

如果您有任何意見請爲他們提供

+0

你問如何增加新的生產線? – Cynede

+2

當您提出問題時,您應該嘗試提供代碼示例,以便我們不必猜測您在做什麼:) – Hardrada

回答

0

要開始一個新行:

Console.WriteLine(); 

例如:

var names = str.Split(); 
for (int i = 0; i < names.Length; i++) 
{ 
    Console.Write(names[i] + '\t'); 
    if ((i + 1) % 4 == 0) 
     Console.WriteLine(); 
} 
3

我會寫東西,管理的填充和佈局..也許是這樣的?

class ConsoleColumnFormatter { 
    private int _columnWidth = 20; 
    private int _numColumns = 4; 

    private int _currentColumn = 0; 

    public ConsoleColumnFormatter(int numColumns, int columnWidth) { 
     _numColumns = numColumns; 
     _columnWidth = columnWidth; 
    } 

    public void Write(string str) { 
     Console.Write(str.PadRight(_columnWidth - str.Length, ' ')); 
     _currentColumn++; 

     checkForNewLine(); 
    } 

    private void checkForNewLine() { 
     if (_currentColumn >= _numColumns) { 
      Console.Write("\n"); 
      _currentColumn = 0; 
     } 
    } 
} 

此:

ConsoleColumnFormatter formatter = new ConsoleColumnFormatter(4, 20); 

for (int i = 1; i <= 10; i++) 
    formatter.Write("Column " + i.ToString()); 

..produces,這樣的:

Column 1 Column 2 Column 3 Column 4 
Column 5 Column 6 Column 7 Column 8 
Column 9 Column 10 
0

您還可以利用的StringBuilder的AppendLine或Environment.NewLine - 輸出到控制檯之前格式化字符串行。

1

這是你應該怎麼寫呢:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<string> names = new List<string>() {"Sam","John","Bob","Adam","Kelly","Nolan","Carl","Tim","Tom","David"}; 

      for (int i = 0; i < names.Count; i++) 
      { 
       if (i % 4 == 0 && i > 0) 
        Console.WriteLine(); 

       Console.Write(names[i] + "\t"); 

      } 

      Console.ReadLine(); 
     } 
    } 
} 

輸出是一樣的,你想

enter image description here

2

如果我理解你的問題。我已經使用以下技術打印在控制檯中輸出Txt(日誌文件)中的表格。

訣竅是使用的String.format

// Example from - http://msdn.microsoft.com/en-us/library/system.string.format.aspx 



using System; 

public class Example 
{ 
    public static void Main() 
    { 
     // Create array of 5-tuples with population data for three U.S. cities, 1940-1950. 
     Tuple<string, DateTime, int, DateTime, int>[] cities = 
      { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277, 
         new DateTime(1950, 1, 1), 1970358), 
      Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995, 
         new DateTime(1950, 1, 1), 7891957), 
      Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808, 
         new DateTime(1950, 1, 1), 3620962), 
      Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452, 
         new DateTime(1950, 1, 1), 1849568) }; 

     // Display header 
     string header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", 
            "City", "Year", "Population", "Change (%)"); 
     Console.WriteLine(header); 
     string output;  
     foreach (var city in cities) { 
     output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}", 
           city.Item1, city.Item2, city.Item3, city.Item4, city.Item5, 
           (city.Item5 - city.Item3)/ (double)city.Item3); 
     Console.WriteLine(output); 
     } 
    } 
} 
// The example displays the following output: 
// City   Year Population Year Population Change (%) 
//  
// Los Angeles  1940 1,504,277 1950 1,970,358  31.0 % 
// New York  1940 7,454,995 1950 7,891,957   5.9 % 
// Chicago   1940 3,396,808 1950 3,620,962   6.6 % 
// Detroit   1940 1,623,452 1950 1,849,568  13.9 %