C#控制台"*"绘制空心菱形

近期公司组织C#初级培训,写了个程序练手。

 

 int n;//菱形上半部分的行数
            Console.WriteLine("请输入一个大于或等于3的奇数");
            n = int.Parse(Console.ReadLine());

            if (n % 2 == 0 || n < 3)
            {
                Console.WriteLine("您输入的值非法请重新输入!");
                Console.ReadKey();
            }

            //上半部分输出n行
            for (int i = 1; i <= n; i++)
            {
                //输出每一行的内容
                for (int j = 1; j <= n + i - 1; j++)
                {  //每行输出内容的具体位置
                    if ((j != n - i + 1) && (j != n + i - 1))
                        Console.Write(" ");
                    else
                        Console.Write("*");
                }
                Console.Write("\n");
            }

            //下半部分输出n-1行
            for (int i = n - 1; i >= 1; i--)
            {
                for (int j = n * 2 - 1; j >= 1; j--)
                {
                    if ((j != n + i - 1) && (j != n - i + 1))
                        Console.Write(" ");
                    else
                        Console.Write("*");
                }
                Console.Write("\n");
            }
            Console.ReadKey();

转载于:https://www.cnblogs.com/Shituo-WQ/archive/2012/04/23/2466347.html

你可能感兴趣的:(C#控制台"*"绘制空心菱形)