是一题笔试题,也没有什么难点,当时写这道题的时候太唐突了,
今天回来再机子上重新完善完善了
题面:请打印以下图形
*
***
*****
*******
*****
***
*
code:
class Program
{
private const string flag = "*";
private const char snull = ' ';
private const int length = 7;
static void Main(string[] args)
{
for (int i = 0; i <= length; i++)
{
if (i % 2 != 0)
PrintFlag(i);
if (i == length)
{
for (int j = length - 2; j > 0; j--)
if (j % 2 != 0)
PrintFlag(j);
}
}
Console.Read();
}
static void PrintFlag(int currentIndex)
{
string t = string.Empty, s = string.Empty;
for (int i = 0; i < currentIndex; i++)
{
t += flag;
}
s = GetLocation(currentIndex, t);
Console.WriteLine(s);
}
static string GetLocation(int currentIndex,string flag)
{
int totalcount = length;
if (currentIndex == totalcount)
return flag;
int startlocation = (totalcount - currentIndex) / 2;
string temp = flag.PadLeft(startlocation + currentIndex, snull);
temp = temp.PadRight(totalcount, snull);
return temp;
}
}

code2
class Program{
static void Main(string[] args)
{ Console.WriteLine(" * ");
Console.WriteLine(" *** ");
Console.WriteLine(" ***** ");
Console.WriteLine("*******");
Console.WriteLine(" ***** ");
Console.WriteLine(" *** ");
Console.WriteLine(" * ");
Console.Read();
}
} code3
Console.WriteLine(@" * *** ***** ******* ***** *** *");
code4
class Program
{
static void Main(string[] args)
{
PrintStar(9);
}
public static void PrintStar(int maxNumber)
{
//可以在此检验maxNumber是否为奇数
///正打
for (int i = 1; i <= maxNumber;)
{
ShowConsoleStar(i,maxNumber);
i = i + 2;
}
//反打
for (int i = maxNumber - 2; i >= 1; )
{
ShowConsoleStar(i,maxNumber );
i = i - 2;
}
}
private static void ShowConsoleStar(int starNumber,int maxNumber)
{
//补位
for (int i = 1; i <= (maxNumber - starNumber)/2;i++ )
{
Console.Write(" ");
}
//打星
for (int i = 1; i <=starNumber; i++)
{
Console.Write("*");
}
//换行
Console.WriteLine();
}
}
http://www.cnblogs.com/wfcfan/archive/2009/10/09/1579758.html
浙公网安备 33010602011771号