【问题标题】:How can I align the text with the text in the next line in console?如何将文本与控制台下一行中的文本对齐?
【发布时间】:2014-11-26 17:25:41
【问题描述】:

有没有一种快捷的方法可以让控制台中第一行的文本与第二行的文本对齐?

示例:

输入你的名字:查尔斯

输入你的学号:20130140

//输出应该是这样的:

   Name      Student Number
  Charles       20130140

这是我的代码,但方法很长。

class MainClass
{
        public static void Main (string[] args)
        {
            int num, nam;
            string snum, name, sec;
            int num1, num2, num3;
            int var;
            int bosh;

        Console.WriteLine ("Enter student number: ");
        snum = Console.ReadLine ();
        Console.WriteLine ("Enter Name: ");
        name = Console.ReadLine ();
        Console.WriteLine ("Enter Section: ");
        sec = Console.ReadLine ();

        num1 = snum.Length;
        num2 = name.Length;
        num3 = sec.Length;

        Console.WriteLine ("        Student Number        Name        Section        ");
        Console.Write("\n");

        if (num1 <= 14) {

            num = (14 - num1) / 2;
            var = (14 - num1) - num;

            for (int i = 0; i < num; i++) {

                Console.Write (" ");
            }
            Console.Write ("        " + snum);

            if (num2 <= 4) {
                nam = (4 - num2) / 2;
                //sum of all sapaces
                bosh = var + nam + 8;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");
                }
                Console.Write (name);
            } else {
                nam = ( num2 - 4) / 2;
                //sum of all sapaces
                bosh = var + (8 - nam);

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");
                }
                Console.Write (name);

            }


        } else if (num1 > 14){

            num = 8 - ((num1 - 14) / 2);

            for (int i = 0; i < num; i++) {

                Console.Write (" ");
            }
            Console.Write (snum);

            if (num2 <= 4) {
                nam = (4 - num2) / 2;
                //sum of all sapaces
                bosh = nam + num;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");

                }

                Console.Write (name);

            } else {

                nam = (num2 - 4) / 2;
                //sum of all sapaces
                bosh = num - nam;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");

                }

                Console.Write (name);

            }

        }

        Console.ReadKey ();

    }

}

【问题讨论】:

标签: c#


【解决方案1】:

.NET 中使用的格式字符串接受an alignment component,允许您指定参数的长度及其对齐方式,例如:

Console.WriteLine("{0,5} {1,-10}",5,10);

第一个参数使用 5 个空格,右对齐,第二个参数使用 10 个空格,左对齐。

在您的情况下,您可以编写如下内容:

Console.WriteLine("{0,-25} {1,-15} {2,10}",name,number,section);

这将在 1-25 列左对齐输出 name,在 27-42 列左对齐输出 number,在 44 列右对齐输出 10 个字符

棘手的部分是,如果格式化字符串的长度大于对齐长度,则忽略对齐。您可以通过在最终输出语句之前首先格式化每个元素,将它们截断为允许的最大长度来解决这个问题。

不幸的是,没有办法居中对齐参数。您必须计算每个格式化字符串的长度并将其填充到代码中。 this SO question 的答案使用函数来居中对齐参数,或者使用自定义 IFormattable 居中对齐格式字符串的参数。

【讨论】:

    猜你喜欢
    • 2011-04-18
    • 2012-05-05
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2013-09-29
    • 2013-11-21
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多