如何使用 Console.WriteLine 对齐列中的文本? [英] How can I align text in columns using Console.WriteLine?

查看:40
本文介绍了如何使用 Console.WriteLine 对齐列中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种列显示,但最后两列似乎没有正确对齐.这是我目前的代码:

I have a sort of column display, but the end two column's seem to not be aligning correctly. This is the code I have at the moment:

Console.WriteLine("Customer name    " 
    + "sales          " 
    + "fee to be paid    " 
    + "70% value       " 
    + "30% value");
for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos = DisplayPos + 1)
{
    seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
    thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);          
    Console.WriteLine(customer[DisplayPos] + "         " 
        + sales_figures[DisplayPos] + "               " 
        + fee_payable[DisplayPos] + "           " 
        + seventy_percent_value + "           " 
        + thirty_percent_value);
}

我是一个菜鸟程序员,所以我可能无法理解给出的所有建议,但如果您有任何建议,我们将不胜感激!

I am a rookie programmer so I may not understand all advice given, but if you have any advice it would be greatly appreciated!

推荐答案

与其尝试手动将文本对齐到带有任意空格字符串的列中,您应该嵌入实际的制表符( 转义符序列)到每个输出字符串中:

Instead of trying to manually align the text into columns with arbitrary strings of spaces, you should embed actual tabs (the escape sequence) into each output string:

Console.WriteLine("Customer name" + "	"
    + "sales" + "	" 
    + "fee to be paid" + "	" 
    + "70% value" + "	" 
    + "30% value");
for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos++)
{
    seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
    thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);          
    Console.WriteLine(customer[DisplayPos] + "	" 
        + sales_figures[DisplayPos] + "	" 
        + fee_payable + "		"
        + seventy_percent_value + "		" 
        + thirty_percent_value);
}

这篇关于如何使用 Console.WriteLine 对齐列中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆