运行结果:

C#打印嵌套菱形问题

程序分析图:

C#打印嵌套菱形问题

代码实现:

static void Print(int row)
        {
            if (row % 2 == 0) return;

            // 重点
            int point = row / 2 + 1;

            for (int i = 1; i <= row; i++)
            {
                for (int j = 1; j <= row; j++)
                {
                    // 打印外部大菱形
                    if ((i + j == point + 1) || (j - i == point - 1) || (i - j == point - 1) ||  (i + j == row + point))                 
                        Console.Write("*");
                    // 打印内部小菱形
                    else if ((i <= point && j <= point) && i + j == point + 3)
                        Console.Write("*");
                    else if ((i <= point && j >= point) && j - i == point - 3)
                        Console.Write("*");
                    else if ((i >= point && j <= point) && i - j == point - 3)
                        Console.Write("*");
                    else if ((i >= point && j >= point) && i + j == row + point - 2)
                        Console.Write("*");
                    else 
                        Console.Write(" ");                    
                }
                Console.WriteLine();
            }
        }

相关文章:

  • 2021-05-24
  • 2021-10-10
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2021-06-04
  • 2021-05-18
  • 2021-12-22
相关资源
相似解决方案