How to print an n-line diamond using C#? Detailed practical explanation

php是最好的语言
Release: 2018-08-09 17:54:39
Original
3114 people have browsed it

Solution idea: First we print a rhombus with n rows. Generally, rhombuses have an odd number of rows (look better), so what we print is a rhombus with an odd number of rows, usually in this form. :

How to print an n-line diamond using C#? Detailed practical explanation

Problem-solving ideas: We can think of the rhombus as two parts, consisting of an equilateral triangle on the top and an inverted triangle on the bottom, and then We can just print them out separately.

            Console.WriteLine("输入一个大于2的正整数");//至少3行才能出一个菱形,输入一个偶数菱形就是这个偶数减1行
            int n = Convert.ToInt32(Console.ReadLine());            for (int i = 1; i <= (n + 1) / 2; i++) //打印菱形的上面部分
            {                for (int j = (n - 1) / 2; j >= i; j--)
                {    //打印空格
                    Console.Write(" ");
                }                for (int k = 1; k <= i * 2 - 1; k++)
                {   //打印“*”号,第i行有i*2-1个“*”号
                    Console.Write("*");
                }
                Console.WriteLine();
            }            for (int i = (n - 1) / 2; i >= 1; i--)//打印菱形下面的部分 与上面部分同理
            {                for (int j = i - 1; j < (n - 1) / 2; j++)
                {
                    Console.Write(" ");
                }                for (int k = 1; k <= i * 2 - 1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
Copy after login

If there is something wrong, please give me some advice and everyone is welcome to ask questions.

Related recommendations:

c How to call PHP functions

Steps for php to call c

#

The above is the detailed content of How to print an n-line diamond using C#? Detailed practical explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!