C# 每行只能显示60个字符

C# 每行只能显示60个字符,c#,C#,运行此程序时,我的第一行有62位,而其他行只有60位。如何在第一行前面放置2个空格,使数组的每行不超过60个字符?所以基本上看起来像(开头有两个空格) 使用系统; 命名空间双阶乘 { 班级计划 { 静态int MAX=5000; //这是告诉程序要乘什么。 私有静态void mult(int[]x,int n) { int y=0,i,m; 对于(i=0;i1;i--) { mult(a,i); } } //这是在计算后替换阶乘。 公共静态无效显示(int[]a) { int i; 对于(i=M

运行此程序时,我的第一行有62位,而其他行只有60位。如何在第一行前面放置2个空格,使数组的每行不超过60个字符?所以基本上看起来像(开头有两个空格)

使用系统;
命名空间双阶乘
{
班级计划
{
静态int MAX=5000;
//这是告诉程序要乘什么。
私有静态void mult(int[]x,int n)
{
int y=0,i,m;
对于(i=0;i1;i--)
{
mult(a,i);
}
}
//这是在计算后替换阶乘。
公共静态无效显示(int[]a)
{
int i;
对于(i=MAX-1;a[i]==0;i--);
控制台。写入(a[i]);
对于(intj=i-1,c=1;j>=0;j--,c++)
{
字符串s=”“+a[j];
控制台。写入(s.PadLeft(4,'0');
如果(c%15==0)
Console.WriteLine();
}
Console.WriteLine();
}
公共静态void Main(字符串[]args)
{
while(true)
{
//告诉用户输入一个数字。
控制台。写入(“输入一个要计算的数字或一个要退出的负数:”);
int n=int.Parse(Console.ReadLine());
//退出功能
如果(n<0)断裂;
int[]arr=新的int[MAX];
阶乘(arr,n);
显示(arr);
}
//100000是它可以计算的最大值
}
}
}

您已经演示了如何使用String.PadLeft(),因此请将正在写入内存的字符串缓存起来,以便检查它们,而不是直接写入控制台

using System.Linq;
using System.Text;

public static void Display(int[] a)
{
    int i = 0;

    // build the string in memory, so we can inspect the length of the lines
    StringBuilder output = new StringBuilder();
    output.Append(a[i]);
    for (int j = i - 1, c = 1; j >= 0; j--, c++)
    {
        output.Append($"{a[j]}".PadLeft(4, '0'));
        if (c % 15 == 0)
            output.AppendLine();
    }

    // get the lines into an array so we can normalise the length,
    // although we know the length today, calculating it expresses the intent better
    var lines = output.ToString().Split(new string [] { Environment.NewLine }, StringSplitOptions.None);
    var maxLength = lines.Max(x => x.Length);

    // now write the output to the console
    foreach(var line in lines)
        Console.WriteLine(line.PadLeft(maxLength, ' '));
}
没有必要像这样使用StringBuilder,您可以使用
列表
,这将进一步简化代码。但是,确定针对
控制台编写的代码可以通过
StringBuilder
轻松重构以写入内存是很有用的,因为语法非常相似

您还可以使用名为
Console
StringWriter
实例,语法相同。。。但对于这样简单的逻辑块来说,这一步太远了


我不太明白,你说我的第一行有62位,而其他行只有60位,但在你的例子中,第一行比其他两行少两个字符?所以我运行了这个程序--我认为这说明了问题所在?我希望它看起来像这个例子。运行当前代码时,第一行显示62个字符,其余行显示60个字符。我需要它们都只有60个,也许你也可以解释一下你想在
显示器中做什么?据我所见,可以做得简单得多。单字母变量看起来超级酷和科学,但它对代码可见性/理解非常有害。如果数组元素中的数字包含前导零,我需要通过打印字符零来填充前导零
    using System;

namespace BigFactorial
{
    class Program
    {
        static int MAX = 5000;
        //This is telling the program what to multiply.
        private static void mult(int[] x, int n)
        {
            int y = 0, i, m;

            for (i = 0; i < MAX; i++)
            {
                m = y + x[i] * n;
                x[i] = m % 10000;
                y = m / 10000;
            }
        }
        //This is telling the program how to calculate factorial of n.
        public static void Factorial(int[] a, int n)
        {
            a[0] = 1;
            for (int i = n; i > 1; i--)
            {
                mult(a, i);
            }
        }
        //This is displaing the factorial after the calculation.
        public static void Display(int[] a)
        {
            int i;
            for (i = MAX - 1; a[i] == 0; i--) ;

            Console.Write(a[i]);
            for (int j = i - 1, c = 1; j >= 0; j--, c++)
            {
                string s = "" + a[j];
                Console.Write(s.PadLeft(4, '0'));
                if (c % 15 == 0)
                Console.WriteLine();
            }
            Console.WriteLine();
        }
        public static void Main(string[] args)
        {
            while (true)
            {
                //Telling user to enter a number.
                Console.Write("Enter a number to factor or a negative number to quit: ");
                int n = int.Parse(Console.ReadLine());

                //Quit function
                if (n < 0) break;
                int[] arr = new int[MAX];
                Factorial(arr, n);
                Display(arr);
            }
            //100000 is the max number it can calculate
        }
    }
}
using System.Linq;
using System.Text;

public static void Display(int[] a)
{
    int i = 0;

    // build the string in memory, so we can inspect the length of the lines
    StringBuilder output = new StringBuilder();
    output.Append(a[i]);
    for (int j = i - 1, c = 1; j >= 0; j--, c++)
    {
        output.Append($"{a[j]}".PadLeft(4, '0'));
        if (c % 15 == 0)
            output.AppendLine();
    }

    // get the lines into an array so we can normalise the length,
    // although we know the length today, calculating it expresses the intent better
    var lines = output.ToString().Split(new string [] { Environment.NewLine }, StringSplitOptions.None);
    var maxLength = lines.Max(x => x.Length);

    // now write the output to the console
    foreach(var line in lines)
        Console.WriteLine(line.PadLeft(maxLength, ' '));
}