c# - 填充字符数组或字符串——寻找更好的方法

标签 c# printing

<分区>

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

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

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

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

我的代码按预期工作,但它非常困惑、无组织,我相信有更好的方法,也许是以前由开发 .NET Framework 的 Microsoft 开发人员在 C# 中实现的方法。

这是我的代码:

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);
}

最佳答案

我喜欢这个问题:)

这是我的做法。真的很期待其他人,因为这真的很有趣:)

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.
    }
}

关于c# - 填充字符数组或字符串——寻找更好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816086/

相关文章:

function - 如何在Rust println中直接使用函数返回值

java - 在java中打印带有一些文本的图像

c# - 使用下拉列表中的选定对象更新字段文本框中的数据

c# - 将 Excel 颜色 BGR 转换为 RGB

c# - 禁用 HttpWebRequest 的图像下载

c# - jquery 幻灯片放映。确定您所在的幻灯片 AKA Div

html - 如何使网页发送到打印机的内容与浏览器窗口中的内容不同?

Java的swing print()用法

javascript - 向网站添加打印按钮(将自动设置缩放、边距和隐藏页眉/页脚以进行打印)

C# 通过组合框使用 SelectedIndexChanged 更改带有文本文件中文本的标签