Making pyramid using c#

后端 未结 13 2128
花落未央
花落未央 2021-01-03 11:04

My question is how to make a pyramid using * and \'space\' in C#? The output will be like this.

     *
    * *
   * * *
  * * * *
 * * * * *
<
相关标签:
13条回答
  • 2021-01-03 11:38

    Post my answer after 3 hours. I think now you have almost finished it under @iluxa's advice?

    int height = 20;
    for (int level = 1; level <= height; level++)
    {
        string text = string.Join(" ", Enumerable.Repeat("*", level));
        Console.WriteLine(text.PadLeft(height - level + text.Length));
    } 
    

    I used some build-in methods e.g. Enumerable.Repeat and String.PadLeft, not the pure C-language way. The purpose is that I want to tell you since you have chosen C# as the programming language(not C/Java/etc), you should resolve problems in the C# way.

    0 讨论(0)
提交回复
热议问题