C# 打印倒三角

void test6(int num)
        {
            try
            {
                #region 方法1
                int maxstar = (num - 1) * 2 + 1;
                string line = "";
                for (int i = num - 1; i >= 0; i--)
                {
                    int star = i * 2 + 1;
                    int diff = maxstar - star;
                    line = "";
                    for (int j = 0; j < star; j++)
                    {
                        line = line + "*";
                    }
                    for (int j = 0; j < diff / 2; j++)
                    {
                        line = " " + line;
                    }
                    Console.WriteLine(line);
                }
                #endregion

                #region 方法2
                int rows = num;
                //循环行数
                for (int i = rows; i > 0; i--)
                {
                    //这个循环式控制对空格的输出 原理:空格数等于 从 0,1,2,...,rows-1 个
                    for (int z = 0; z < rows - i; z++)
                    {
                        Console.Write(" ");
                    }
                    //这个循环控制 "*" 的输出,原理:个数=2*行-1
                    for (int j = 0; j < 2 * i - 1; j++)
                    {
                        Console.Write("*");
                    }
                    Console.Write("\r\n");//换行
                }
                #endregion
            }
            catch (Exception)
            {

                //throw;
            }
        }

  

posted @ 2017-07-03 17:18  dyfisgod  阅读(621)  评论(0编辑  收藏  举报