Console.Write("请输入一个数字:"); try { int a = Convert.ToInt32(Console.ReadLine()); if (a % 2 != 0) { for (int i = 1; i <= (a+1)/2; i++)//菱形上半部分 { int c = ((i * 2 - 1) + 1) / 2 - 1;//中间值 string end = ""; char b=‘a‘; int count = 0; bool d = false; for (int j = 1; j <=(a+1)/2-i; j++) { end += " "; } for (int j = 1; j <= i * 2 - 1; j++) { end += b; if (count == c) { d = true; } if (d) { if (b == ‘A‘) { b = ‘Z‘; } else { b--; } } else { if (b == ‘Z‘) { b = ‘A‘; } else { b++; } count++; } } Console.WriteLine(end); } for (int i = 1; i < (a + 1) / 2; i++)//菱形的下半部分 { char b = ‘a‘; int c = ((a - i * 2)+1) / 2 - 1; string end = ""; int count = 0; bool d = false; for (int j = 1; j <= i; j++) { end += " "; } for (int j = 1; j < (a - i * 2) + 1; j++) { end += b; if (count == c) { d = true; } if (d) { if (b == ‘A‘) { b = ‘Z‘; } else { b--; } } else { if (b == ‘Z‘) { b = ‘A‘; } else { b++; } count++; } } Console.WriteLine(end); } } else { Console.Write("输入错误"); } } catch { Console.Write("输入错误"); } Console.ReadLine();
原文:http://www.cnblogs.com/zoubizhici/p/5554861.html