1. 程式人生 > >一個FOR迴圈的等腰三角形

一個FOR迴圈的等腰三角形

  public static void Main()
        {
            int num = 0;
            Console.Write("請輸入一個奇數:");
            num = Convert.ToInt32(Console.ReadLine());
            Print(num, (num + 1) / 2, 1);
            Console.WriteLine("");
        }

        static void Print(int num, int position, int count)
        {
            int temp = count;
            for (int i = 1; i <= num; i++)
            {
                if (position == i && count != 0)
                {
                    position++;
                    count--;
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
                if (i == num)
                {
                    position = position - temp;
                    count = temp + 2;
                    position = position - 1;
                    if (count > num) break;
                    Console.WriteLine("");
                    Print(num, position, count);
                }
            }
        }