填充字符数组或字符串 - 寻找更好的方式

时间:2015-11-19 23:11:24

标签: c# printing

我写了一个非常简单的C#代码,执行以下任务:

给出一个代表空格数量的n,我的程序将打印沙漏:

*****
 ***
  *
 ***
*****

从命令窗口的左边界墙开始n个空格。

我的代码按预期工作,但它非常混乱,杂乱无章,而且我确信有一种更好的方法,也许是以前在C#中由开发.NET Framework的微软开发人员实现的。

这是我的代码:

public static void printAsterisk(int initialSpace)
{
    int i, j;
    char[] asteriskMsg = new char[5 * initialSpace + 25];
    for (i = 0; i < initialSpace; i++)
    {
        asteriskMsg[i] = ' ';
    }
    for (j = i; j < i + 5; j++)
    {
        asteriskMsg[j] = '*';
    }
    asteriskMsg[j] = '\n';
    for (i = j + 1; i < j + initialSpace + 1; i++)
    {
        asteriskMsg[i] = ' ';
    }
    for (j = i + 1; j < i + 4; j++)
    {
        asteriskMsg[j] = '*';
    }
    asteriskMsg[j] = '\n';
    for (i = j + 1; i < j + initialSpace + 3; i++)
    {
        asteriskMsg[i] = ' ';
    }
    asteriskMsg[i] = '*';
    asteriskMsg[i + 1] = '\n';
    for (j = i + 2; j < i + initialSpace + 3; j++)
    {
        asteriskMsg[j] = ' ';
    }
    for (i = j; i < j + 3; i++)
    {
        asteriskMsg[i] = '*';
    }
    asteriskMsg[i] = '\n';
    for (j = i + 1; j < i + initialSpace + 1; j++)
    {
        asteriskMsg[j] = ' ';
    }
    for (i = j; i < j + 5; i++)
    {
        asteriskMsg[i] = '*';
    }
    string s = new string(asteriskMsg);
    System.Console.Write("{0}", s);
}

}

这是非常可怕的。我很好奇如何改进这段代码。 只是为了展示一个我的意思的例子,这就是程序与n=25一起运行的方式 enter image description here

如果n为零,那么你会看到相同的图片,但是从左墙开始,而不是25个空格。我希望现在很清楚我想要做什么。

编辑:管理自己使用字符串来提出一个很好的解决方案!

public static void printAsterisk(int initialSpace)
{
    string firstRow = "*****", secondRow = "***", thirdRow = "*", firstRowSpaces = new string(' ', initialSpace), secondRowSpaces = new string(' ', initialSpace+1), thirdRowSpaces = new string(' ', initialSpace+2);
    string hourglass = string.Format("{0}{1}\n{2}{3}\n{4}{5}\n{2}{3}\n{0}{1}", firstRowSpaces, firstRow, secondRowSpaces, secondRow, thirdRowSpaces, thirdRow);
    System.Console.WriteLine(hourglass);
}

1 个答案:

答案 0 :(得分:3)

我喜欢这个问题:)

这是我这样做的方式。真的很期待别人,因为这真的很有趣:)

public class Program
{
    public static void Main(string[] args)
    {
        PrintHourGlass(25);
    }

    /// <summary>
    /// Prints an hour glass starting at the given offset
    /// </summary>
    /// <param name="indent">The starting offset</param>
    static void PrintHourGlass(int indent)
    {
        PrintAsteriks(indent, 5); // offset = indent, print 5 *          [I]*****
        PrintAsteriks(indent + 1, 3); // offset = indent + 1, print 3 *  [I] ***
        PrintAsteriks(indent + 2, 1); // offset = indent +3, print 1 *   [I]  *
        PrintAsteriks(indent + 1, 3); // offset = indent + 1, print 3 *  [I] ***
        PrintAsteriks(indent, 5); // offset = indent, print 5 *          [I]*****
    }

    /// <summary>
    /// Prints given number of * characters starting from the given offset
    /// </summary>
    /// <param name="indent">The starting offset</param>
    /// <param name="asterisks">The number of * characters to print</param>
    static void PrintAsteriks(int indent, int asterisks)
    {
        // Check starters guide to string.Format here:
        // https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx#Starting

        // string format uses a template and allows you to pass in and format arguments as you like.
        // The template below evaluates to {0,25}{1} for indent = 25 and asteriks = 5
        // which means,
        // print argument 0 (first parameter), no extra formatting, pad the output to 25 characters
        // print argument 1, no formatting
        string formatString = "{0," + indent + "}{1}";

        // Console.WriteLine method uses string.Format internally
        // Below, for the template, argument 0 is null
        //     (since we want to print only 25 characters, the padding.
        //     The value could have been "" instead of null)
        // Argument 1 is a string of * characters, with the length specified by asteriks parameter
        Console.WriteLine(formatString, null, new string('*', asterisks));

        // Therefore, it outputs 25 linear white spaces, then 5 * characters.
    }
}