//BOSS:
//让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成
//比如用户输入了7
// A
// ABA
// ABCBA
// ABCDCBA
// ABCBA
// ABA
// A
//1、接收并判断用户输入的是不是数字
try
{
#region 解法一
//Console.Write("请输入一个奇数:");
//int a = Convert.ToInt32(Console.ReadLine());
//if (a % 2 != 0)
//{
// for (int i = 1; i <= (a + 1) / 2; i++)
// {
// for (int b = 1; b <= ((a + 1) / 2 - i); b++)
// {
// Console.Write(" ");
// }
// char c = 'A';
// for (int d = 1; d < i; d++)
// {
// Console.Write(c);
// c++;
// }
// for (int e = 1; e <= 26; e++)
// {
// Console.Write(c);
// if (c == 'A')
// {
// break;
// }
// c--;
// }
// Console.WriteLine();
// }
// for (int i = 1; i < (a + 1) / 2; i++)
// {
// for (int b = 1; b <= i; b++)
// {
// Console.Write(" ");
// }
// char c = 'A';
// for (int d = 1; d < (a + 1) / 2 - i; d++)
// {
// Console.Write(c);
// c++;
// }
// for (int e = 1; e <= 26; e++)
// {
// Console.Write(c);
// if (c == 'A')
// {
// break;
// }
// c--;
// }
// Console.WriteLine();
// }
//}
#endregion
#region 解法二 上半部分
Console.Write("请输入一个奇数:");
int a = Convert.ToInt32(Console.ReadLine());
if (a % 2 != 0)//是奇数,执行这个if里面的代码
{
for (int i = 1; i <= (a + 1) / 2; i++)//上半部分行数,(a+1)/2 代表 上半部分需要打印的行数。
{
char ch = 'A';
string end = "";
int b = ((i * 2 - 1) + 1) / 2 - 1;//开始 -- 的数值
bool isok = false;
int count = 0;
for (int j = 1; j <= ((a + 1) / 2) - i; j++)//拼接每行打印的空格数
{
end += " ";
}
for (int j = 1; j <= i * 2 - 1; j++)//拼接每行打印的字母数
{
end += ch;
if (count == b)//判断是不是该 -- 了
{
isok = true;
}
if (isok)//满足条件,执行这个 if 里面的代码
{
if (ch == 'A')
{
ch = 'Z';
}
else//条件不成立
{
ch--;
}
}
else
{
if (ch == 'Z')
{
ch = 'A';
}
else
{
ch++;
}
count++;
}
}
Console.WriteLine(end);
}
}
#endregion
#region 解法二 下半部分
for (int i = 1; i < (a + 1) / 2; i++)
{
char ch = 'A';
string end1 = "";
int b = ((a - i * 2) + 1) / 2 - 1;
int count = 0;
bool isok = false;
for (int j = 1; j <= i; j++)
{
end1 += " ";
}
for (int j = 1; j < (a - i * 2) + 1; j++)
{
end1 += ch;
if (count == b)
{
isok = true;
}
if (isok)
{
if (ch == 'A')
{
ch = 'Z';
}
else
{
ch--;
}
}
else
{
if (ch == 'Z')
{
ch = 'A';
}
else
{
ch++;
}
count++;
}
}
Console.WriteLine(end1);
}
#endregion
}
catch
{
Console.WriteLine("输入有误!");
}
Console.ReadLine();
![]()