簡體   English   中英

在控制台應用程序中格式化輸出

[英]Formatting output in console application

我在格式化字符串時遇到麻煩:

Console.WriteLine("SSN: {0}   Gross: {1}   Tax: {2}", t[x].SSN, t[x].Gross.ToString("C"), t[x].Tax.ToString("C"));

它應該打印:

SSN            Gross         Tax
123456789    $30,000.00     $8400.00

完整的代碼如下:

using System;

namespace TaxPayerDemo
{
    class Program
    {
        static void Main()
        {
            TaxPayer[] t = new TaxPayer[10];
            int x;

            for(x = 0; x < t.Length; ++x)
            {
                // Stores SSN 
                t[x] = new TaxPayer();
                Console.Write("Please enter your SSN >> ");
                t[x].SSN = Console.ReadLine();

                // Stores Gross Income
                Console.Write("Please enter your income >> ");
                t[x].Gross = Convert.ToDouble(Console.ReadLine());
            }

            for (x = 0; x < t.Length; ++x)
            {
                t[x].calcTax();
                Console.WriteLine();
                Console.WriteLine("SSN: {0}   Gross: {1}   Tax: {2}", t[x].SSN, t[x].Gross.ToString("C"),
                    t[x].Tax.ToString("C"));
                         }
            Console.ReadKey();
        }
    }

    class TaxPayer
    {
        private const double LOW_TAXRATE = 0.15;
        private const double HIGH_TAXRATE = 0.28;

        public double Tax { get; set; }
        public double Gross { get; set; }
        public string SSN { get; set; }

        public void calcTax()
        {
            if (Gross < 30000)
            {
                Tax = LOW_TAXRATE * Gross;
            }
            if (Gross >= 30000)
            {
                Tax = HIGH_TAXRATE * Gross;
            }
        }
    }
}

請查看“ 復合格式”以獲取有關字符串格式如何工作的信息。 為了對齊文本,請使用格式{0,min_width}

Console.WriteLine("SSN            Gross          Tax");
Console.WriteLine("{0,-15}{1,-15}{2,-15}", t[x].SSN, t[x].Gross.ToString("C"), t[x].Tax.ToString("C"));

這將使值與標題對齊。 隨意將15更改為任何其他值; 只需記住在標題中添加正確的間距(“ SSN”,“ Gross”和“ Tax”之間)。

請注意,我使用的最小寬度為-15。 -表示文本將保持左對齊。 請改用正數以使其正確對齊。

我將為每行(包括標題)使用相同的格式字符串。 要獲得這樣的結果:

SSN Number   Gross Income  Taxes
172-00-1234  $30,000.00    $8,400.00
137-00-7263  $38,000.00    $8,800.00
138-00-8271  $27,000.00    $7,300.00

使用如下代碼:

public struct TaxPayer
{
    public string SSN { get; set; }
    public decimal Gross { get; set; }
    public decimal Tax { get; set; }        

}
class Program
{
    static void Main(string[] args)
    {
        var list=new List<TaxPayer>() { 
            new Person() { SSN="172-00-1234", Gross=30000m, Tax=8400m },
            new Person() { SSN="137-00-7263", Gross=38000m, Tax=8800m }, 
            new Person() { SSN="138-00-8271", Gross=27000m, Tax=7300m }, 
        };
        //each number after the comma is the column space to leave
        //negative means left aligned, and positive is right aligned
        string format="{0,-12} {1,-13} {2,-13}";
        string[] heading = new string[] { "SSN Number", "Gross Income", "Taxes" };

        Console.WriteLine(string.Format(format, heading));
        for (int i = 0; i < list.Count; i++)
        {
            string[] row=new string[] { list[i].SSN, list[i].Gross.ToString("C"), list[i].Tax.ToString("C") };
            Console.WriteLine(string.Format(format, row));
        }
    }
}

當然,我會考慮使用static方法在TaxPayer中盡可能地移動所有格式代碼以生成標頭,並且每個實例都調用.ToString()

public struct TaxPayer
{
    public string SSN { get; set; }
    public decimal Gross { get; set; }
    public decimal Tax { get; set; }

    public static string Formatting="{0,-12} {1,-13} {2,-13}";

    public static string Heading { get { return string.Format(Formatting, new string[] { "SSN Number", "Gross Income", "Taxes" }); } }
    public override string ToString()
    {
        return string.Format(Formatting,
            new string[] { SSN, Gross.ToString("C"), Tax.ToString("C") });
    }
}

    Console.WriteLine(TaxPayer.Heading);
    for (int i = 0; i < list.Count; i++)
    {
        Console.WriteLine(list[i]);
    }

對於Enter輸入\\n而不是空格輸入\\t

Console.WriteLine("SSN:\t\tGross:\t\tTax:\t\t\n{0}\t\t{1}\t\t{2}", t[x].SSN, t[x].Gross.ToString("C"),
        t[x].Tax.ToString("C"));

我使用了兩個選項卡( \\t\\t ),所以它更加清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM