金字塔在C#澄清

时间:2016-11-24 23:24:24

标签: c#

我是C#的初学者,学习循环。在我从中学习的书中告诉我们用星号建造一个金字塔。完全踩了一下,我看着答案键,变得更加困惑。这是关键代码:

这是答案代码

This is the answer code

没有真正的逐步示例代码正在做什么,我不明白程序员是如何找到这个解决方案或他们在评论中解释的内容。有人可以解决这个问题吗?

2 个答案:

答案 0 :(得分:4)

首先可视化所需的输出:

Line 0 has 4 spaces then 1 star  (and optionally 4 more spaces).
Line 1 has 3 spaces then 3 stars (and optionally 3 more spaces).
Line 2 has 2 spaces then 5 stars (and optionally 2 more spaces).

然后将其分解为如何打印每一行。

What comes next in this sequence (and why): 4,3,2,?
What comes next in this sequence (and why): 1,3,5,?

然后意识到这是两个不同的问题。

NumberOfSpaces = 5 - lineNumber -1
NumberOfStars = 2 * lineNumber + 1

然后你编码。

namespace PositiveOrNegative
{
  class Program
  {
    static void Main(string[] args)
    {
      for(int lineNumber = 0; lineNumber < 4; lineNumber++)
      {
        int NumberOfSpaces = 5 - lineNumber - 1;
        int NumberOfStars = 2 * lineNumber + 1;

        for(int space = 0; space < NumberOfSpaces; space++)
        {
          Console.Write(" ");
        }

        for(int star = 0; star < NumberOfStars; star++)
        {
          Console.Write("*");
        }

        Console.WriteLine();
      }
      Console.ReadKey();
    }
  }
}

把它放在一个循环中,lineNumber从0到4,你完成了,这就是他所做的。这个程序做了同样的事情:

namespace PositiveOrNegative
{
  class Program
  {
    static void Main(string[] args)
    {
      int lineNumber;
      int NumberOfSpaces;
      int NumberOfStars;

      lineNumber = 0;
      NumberOfSpaces = 5 - lineNumber - 1;
      NumberOfStars = 2 * lineNumber + 1;
      Console.WriteLine(new String(' ',NumberOfSpaces) + new String('*',NumberOfStars));

      lineNumber = 1;
      NumberOfSpaces = 5 - lineNumber - 1;
      NumberOfStars = 2 * lineNumber + 1;
      Console.WriteLine(new String(' ',NumberOfSpaces) + new String('*',NumberOfStars));

      lineNumber = 2;
      NumberOfSpaces = 5 - lineNumber - 1;
      NumberOfStars = 2 * lineNumber + 1;
      Console.WriteLine(new String(' ',NumberOfSpaces) + new String('*',NumberOfStars));

      lineNumber = 3;
      NumberOfSpaces = 5 - lineNumber - 1;
      NumberOfStars = 2 * lineNumber + 1;
      Console.WriteLine(new String(' ',NumberOfSpaces) + new String('*',NumberOfStars));

      lineNumber = 4;
      NumberOfSpaces = 5 - lineNumber - 1;
      NumberOfStars = 2 * lineNumber + 1;
      Console.WriteLine(new String(' ',NumberOfSpaces) + new String('*',NumberOfStars));

      }
      Console.ReadKey();
    }
  }
}

如果你讨厌循环,那么你可以这样做:

static String  valueOf(char[] data)

答案 1 :(得分:0)

我已经解释了程序中的金字塔逻辑。寻找以下评论。

核心逻辑:

第一个循环:将循环计数设置为金字塔中行的数字 第二个循环(空格):将循环计数设置为每行*之前所需的空格 第三个循环(星号):将循环计数设置为*每行所需*

有关详细信息,请参阅以下计划中的评论。

class Program
{
    static void Main(string[] args)
    {
        /// This for loop is for keeping the number of rows in pyramid (height) and - 
        /// iterate through each rows to print the pyramid using *
        for(int row=0;row<5;row++)
        {
            /// This loop is used add spaces before *
            /// Core logic - Set number of loop (space max) as number spaces required
            /// Row 1: Adding 4 spaces before * (space max limit = 5 - 0 - 1)
            /// Row 2: Adding 3 spaces before * (space max limit = 5 - 1 - 1)
            /// Row 3: Adding 2 spaces before * (space max limit = 5 - 2 - 1)
            /// And goes on like above pattern
            for (int space=0;space<5-row-1;space++)
            {
                Console.Write(" ");
            }

            /// This loop is used add *
            /// Building the * pattern, logic in below comments
            /// Core logic - Set number of loop (star max) as number for stars required
            /// First row: Stars required = 1, (star max limit = 0 * 2 + 1 = 1)
            /// Second row: Stars required = 3, (star max limit = 1 * 2 + 1 = 3)
            /// Third row: Stars required = 5, (star max limit = 2 * 2 + 1 = 5)
            /// Fourth row: Stars required = 7, (star max limit = 3 * 2 + 1 = 7)
            /// And goes on like above pattern
            for (int star = 0; star < row * 2 + 1; star++)
            {
                Console.Write("*");
            }

            Console.WriteLine();
        }

        Console.ReadKey();
    }
}