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. :
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(); }
If there is something wrong, please give me some advice and everyone is welcome to ask questions.
Related recommendations:
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!