写出具有特定形状的星星

时间:2011-07-20 14:19:03

标签: c#

我想写一个形状为“*”和“|”的形状如下。 程序必须取user.Width的高度和宽度,不带'|的列号'。我试着写但很困惑。我的代码有时效果很好而且有时候很愚蠢。例如当我输入高度:13,宽度:4时它再写一个,如果witdh为1则进入无限循环。试图解决它变得太矛盾了。我必须修理或重写吗?这是代码:height = 10,width = 5

|*____|    
|_*___|
|__*__|
|___*_|
|____*|
|___*_|
|__*__|
|_*___|
|*____|
|_*___|
      private static void Function()
      {
        int height, width;

        if (width == 2)
            while (height > 0)
            {
                FirstPart(width, height);
                height -= width;
            }
        else
            while (height > 0)
            {
                if (height > 1)
                {
                    FirstPart(width, height);
                    height -= width;
                }
                if (height > 0)
                {
                    SecondPart(width, height);
                    height -= width - 2;
                }
            }
    }


    private static void FirstPart(int width,int height)
    {

        if(height > width)
             for (int i = 0; i < width; i++)
             {
                for (int j = 0; j < width+2; j++)
                {

                    if (j == 0 || j == width + 1)
                        Console.Write("|");
                    else
                         if (i + 1 == j)
                            Console.Write("*");
                         else
                             Console.Write(" ");
                }
             Console.WriteLine();
        }
        else
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width + 2; j++)
                {

                    if (j == 0 || j == width + 1)
                        Console.Write("|");
                    else
                        if (i + 1 == j)
                            Console.Write("*");
                        else
                            Console.Write(" ");
                }
                Console.WriteLine();
            }
    }
    private static void SecondPart(int width,int height)
    {

        if(height > width)

            for (int i = 0; i < width-2; i++)
            {
               for (int j = 0; j < width+2; j++)
               {

                if (j == 0 || j == width + 1)
                    Console.Write("|");
                else
                    if (i + j == width-1)
                        Console.Write("*");
                    else
                        Console.Write(" ");
            }
            Console.WriteLine();
        }
        else
            for (int i = 0; i < height; i++)
            {                                     
                for (int j = 0; j < width + 2; j++)
                {
                    if (j == 0 || j == width + 1)
                        Console.Write("|");
                    else
                        if (i + j == width - 1)
                            Console.Write("*");
                        else
                            Console.Write(" ");
                }
                Console.WriteLine();
            }
    }            

6 个答案:

答案 0 :(得分:2)

    private static void WriteStars(int width, int height)
    {
        int j = 0;
        for (int i = 0; i < height; i++)
        {
            Console.Write("|");
            for (int f = 0; f < width; f++)
            {
                if (f == Math.Abs(j))
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }
            j++;
            if (Math.Abs(j) == width - 1)
            {
                j *= -1;
            }
            Console.WriteLine("|");
        }
    }

可能会因为给你一个完整的答案而被贬低,但也许它会告诉你一个正确的方法,你可以从中学到一些东西......

答案 1 :(得分:1)

我看到了

while (Height > 0)

所以你的无限循环来自高度永远不会小于或等于0。

答案 2 :(得分:0)

最好重写一下。当你这样做时,将代码分离成几个函数,以便一个函数绘制一行,另一个函数调用前者绘制所有行。

答案 3 :(得分:0)

void WriteStars(int Width,int Height)
{
    int _sp=1; //Star Pos
    bool _left = false;
    for(int i =0;i<Height;i++)
    {
        Console.Write("|");
        int j;
        for(j=1;j<Width-1;j++)
        {
            if(j==_sp)
            {
                Console.Write("*");
                if(_left)
                {
                    _sp--;
                }
                else
                {
                    _sp++;
                }
                   j++;
                   break;
            }
            else
            {
               Console.Write("_");
            }
        }
        for(;j<Width-1;j++)
        {
            Console.Write("_");
        }

        Console.WriteLine("|");
        if(_sp==0)
        {
            _left = false;
        }
        else if(_sp==Width)
        {
            _left = true;
        }

    }
}

尝试它是否有效,在这里写下来。

答案 4 :(得分:0)

更短:

static void Variante_2(int height, int width)
{
  byte[][] arr = new byte[height][];
  int pos = 0;
  int mov = 1;
  for (int line = 0; line < height; line++)
  {
    arr[line] = new byte[width];
    for (int col = 0; col < width; col++) { arr[line][col] = 45; }
    arr[line][pos] = 42;
    pos += mov;
    if (pos == 0 || pos == (width - 1)) { mov *= -1; }
    Console.WriteLine("|" + ASCIIEncoding.ASCII.GetString(arr[line]) + "|");
  }
  string temp = Console.ReadLine();
}

答案 5 :(得分:0)

可以用更少的代码完成:

static void Variante_3(int height, int width)
{
    int pos = 1;
    int mov = 1;
    for (int line = 0; line < height; line++)
    {
        Console.WriteLine("|" + "*".PadLeft(pos, '_') + "|".PadLeft(width - pos, '_'));
        pos += mov;
        if (pos == 1 || pos == (width - 1)) { mov *= -1; }
    }
    string temp = Console.ReadLine();
}

很抱歉所有人都没有做其他功课,但是我没睡觉就没睡觉 g