在C3中创建等腰三角形

时间:2017-08-14 14:39:54

标签: c#

下面的代码只会形成一个直角三角形,我怎么把它变成等腰三角形呢?

int height = 4;
string star = "";
for (int i = 0; int i < height; i++)
{
    star += "*";
    Console.WriteLine(star);
}
Console.ReadLine();

这只显示一个直角三角形。我试图制作的是金字塔。

3 个答案:

答案 0 :(得分:3)

这里有一个更干净的代码:

int numberoflayer = 4;
int empty;
int number;
for (int i = 1; i <= numberoflayer; i++)
{
    for (empty = 1; empty <= (numberoflayer - i); empty++)
        Console.Write(" ");
    for (number = 1; number <= i; number++)
        Console.Write('*');
    for (number = (i - 1); number >= 1; number--)
        Console.Write('*');
    Console.WriteLine();
}

答案 1 :(得分:1)

这会吸引你的圣诞树:

int height = 4;
for (int i = 0; i < height; i++)
{
    int countSpaces = (int)Math.Ceiling((height * 2 / 2d) - i);
    int countStars = 1 + (i * 2);
    string line = new string(' ', countSpaces) + new string('*', countStars);
    Console.WriteLine(line);
}

答案 2 :(得分:0)

脏代码,但你去了

int height = 4;
string empty = "    ";
String star = "";
for(int i = 0; i<height; i++)
{
    star += " *";
    empty = empty.Length > 0 ? empty.Remove(0,1) : " ";
    Console.WriteLine(empty + star);
}
Console.ReadLine();