您的位置:首页 > 编程语言 > C#

C#题型补充

2016-06-02 23:59 429 查看

让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成

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();

用户输入一个字母,如D,那么就打印 ABCDCBA

//用户输入一个字母,如D,那么就打印 ABCDCBAConsole.Write("请输入一个字母");char a = Convert.ToChar(Console.ReadLine());char b = 'a';bool c = true;//判断b++或是b--//拼接要打印的内容for (int i = 1; i <= 26; i++){Console.Write(b);if (b == a){c= false;}if (c){b++;}else{b--;if (b == 'a' || b == 'A'){Console.Write(b);break;}}}Console.ReadLine();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: