在 ConsoleApplication 中输出自定义形状 - C#

发布于 2024-09-04 16:19:23 字数 428 浏览 2 评论 0原文

我有这个形状,我想将其输出到 ConoleApplication Windows。

替代文本

我有这段代码,但它不能按我的需要工作:

int i,j;

for(i=0;i<5;i++)

{

for(j=5-i;j>0;j--)

Console.WriteLine(" ");

for(j=0;j<=2*i;j++)

Console.WriteLine("*");

Console.WriteLine("\n");

}

提前致谢。

编辑:我很抱歉

I have this shape and I want to output it to ConoleApplication Windows.

alt text

I have this code but it doesn't work as I need :

int i,j;

for(i=0;i<5;i++)

{

for(j=5-i;j>0;j--)

Console.WriteLine(" ");

for(j=0;j<=2*i;j++)

Console.WriteLine("*");

Console.WriteLine("\n");

}

Thanks in advance.

EDIT : I am very sorry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

渡你暖光 2024-09-11 16:19:23

众所周知,使用 LINQ 时一切都可以正常工作...那么,您是否尝试过使用 LINQ 来实现这一点?

int n = 6;

var result = string.Join("\r\n", from i in Enumerable.Range(1, n)
                                 where i != 2
                                 let stars = Enumerable.Repeat('*', i)
                                 let indent = new string(' ', n - i)
                                 select indent + string.Join(" ", stars));

Console.WriteLine(result);
     *
   * * *
  * * * *
 * * * * *
* * * * * *

As everyone knows, everything Just Works™ when using LINQ... so, have you tried doing it with LINQ?

int n = 6;

var result = string.Join("\r\n", from i in Enumerable.Range(1, n)
                                 where i != 2
                                 let stars = Enumerable.Repeat('*', i)
                                 let indent = new string(' ', n - i)
                                 select indent + string.Join(" ", stars));

Console.WriteLine(result);
     *
   * * *
  * * * *
 * * * * *
* * * * * *
孤星 2024-09-11 16:19:23

像这样的东西:

            for (j = 0; j <= 2 * i; j += 2)
            {
                printf("*");
                printf(" ");
                // or Console.Write("* ") if we are talking C#
            }

它在星号之间写入空格(加上一个备用的;如果它很重要,你可以删除它)。

Something like:

            for (j = 0; j <= 2 * i; j += 2)
            {
                printf("*");
                printf(" ");
                // or Console.Write("* ") if we are talking C#
            }

which writes the spaces between asterisks (plus a spare one; you could remove that if it is important).

闻呓 2024-09-11 16:19:23

您刚刚使用了 WriteLine 而不是 Write

这是正确的代码:

    int i, j;

    for (i = 0; i < 5; i++)
    {

        for (j = 5 - i; j > 0; j--)
            Console.Write(" ");

        for (j = 0; j <= 2 * i; j++)
            Console.Write("*");

        Console.WriteLine();

    }

You just used WriteLine instead of Write

Here is the correct code:

    int i, j;

    for (i = 0; i < 5; i++)
    {

        for (j = 5 - i; j > 0; j--)
            Console.Write(" ");

        for (j = 0; j <= 2 * i; j++)
            Console.Write("*");

        Console.WriteLine();

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文