c# 带星号和空格的循环

c# Loops with stars and spaces

我目前正在为我必须做的这个简单的循环分配而烦恼。

基本上我要实现的是:

1) 用户输入星形金字塔的长度

2) 用for循环做一个金字塔

它需要看起来像这样: (如果需要5层高,第一排5格1星,第二排4格2星,依此类推

    *
   **
  *** 
 ****

(很难格式化,但你明白我的意思。)

我目前有这个

    public void Pyramid()
    {
        Console.WriteLine("Give the hight of the pyramid");
        _aantal = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= _aantal; i++) // loop for hight
        {
            for (int d = _aantal; d > 0; d--) // loop for spaces
            {
                Console.Write(_spatie);
            }

            for (int e = 0; e < i; e++) // loop for stars
            {
                Console.Write(_ster);
            }

            Console.WriteLine();
        }
    }

输出始终是插入的空格数,并且没有正确递减。 虽然如果我调试它它会正确倒计时。

感谢您的回复。

您可以使用字符串 class 的构造函数为您创建重复,然后一次打印两个值,这样就不需要额外的 for 循环了

static void Main(string[] args)
{
    int rowHeight = 5;
    for (int row = 1; row <= rowHeight; row++)
    {
        string spaces = new string(' ', rowHeight - row);
        string stars = new string('*', row);
        Console.WriteLine("{0}{1}", spaces, stars);
    }
    Console.ReadLine();
}

更新

对于语义,我还会用 2 个 for 循环来展示它

static void Main(string[] args)
{
    int rowHeight = 5;
    for (int row = 1; row <= rowHeight; row++)
    {
        int totalSpaces = rowHeight - row;
        for (int j = 0; j < totalSpaces; j++)
        {
            Console.Write(" ");
        }
        for (int j = 0; j < row; j++)
        {
            Console.Write("*");
        }
        Console.WriteLine();
    }
    Console.ReadLine();
}

嗯,你的问题是

for (int d = _aantal; d > 0; d--) // loop for spaces

你很想

for (int d = _aantal - i ; d > 0; d--) // loop for spaces

但它实际上只是反映了您当前拥有的内容,仍然没有创建您似乎想要的金字塔外观。

我认为您在控制台应用程序中最接近的是每隔一行减去一个 space:

for (int d = _aantal-i; d > 0; d-=2) // loop for spaces

这将给出输出:

Give the hight of the pyramid: 10

     *
    **
    ***
   ****
   *****
  ******
  *******
 ********
 *********
**********

知道了!

static void Main(string[] args)
    {
        Console.WriteLine("Give the hight of the pyramid");
        string _spatie = " ";
        string _ster = "*";
        int _aantal = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= _aantal; i++) // loop for height
        {

            for (int d = i; d < _aantal; d++) // loop for spaces
            {
                Console.Write(_spatie);
            }

            for (int e = 1; e <= i; e++) // loop for stars
            {
                Console.Write(_ster);
            }

            Console.WriteLine();
        }

        Console.ReadKey();
    }

看看这个..!!您错过了空间循环内高度循环的迭代器 'i'。

你会得到三角形:-

   *
  **
 ***
****

对称金字塔需要奇数颗星星。

我知道你想把它作为一个控制台应用程序来做,但如果你修改这段代码,它应该可以正常工作

用控制台替换textbox1/2。Readline/Write

    int pyramidstories = int.Parse(TextBox2.Text);
    int I = 1;

    while (I <= pyramidstories)
    {
        for (int spacecount = 0; spacecount < (pyramidstories - I); spacecount++)
        {
            TextBox1.Text += " ";
        }

        for (int starcount = 1; starcount < I + 1; starcount++)
        {
            TextBox1.Text += "*";
        }

        TextBox1.Text += Environment.NewLine;

        I++;
    }

正如您的问题所述,您需要:
4 个空格 1 个星
3 个空格 2 个星
2 个空格 3 个星
等..

所以你的金字塔应该看起来像

       *
      **
     ***
    ****
   *****

上面的代码示例显示了一个金字塔,如上图所示

要像这样获得金字塔(具有适当的间距):

您可以使用:

    static void Main(string[] args)
    {
        // The length of the pyramid
        int lengte = 18;

        // Loop through the length as given by the user
        for (int i = 0; i <= lengte; i++)
        {
            // If its an even number (we don't want 1-2-3.. but 1-3-5.. stars)
            if (i % 2 == 0)
            {
                // Calculate the length of the spaces we need to set
                int spatieLengte = (lengte / 2) - (i / 2);
                // Display spaces
                for (int spaties = 0; spaties <= spatieLengte; spaties++)
                {
                    Console.Write(" ");
                }
                // Display stars
                for (int sterren = 0; sterren <= i; sterren++)
                {
                    Console.Write("*");
                }
                // Newline
                Console.WriteLine();
            }
        }
        Console.ReadLine();
    }

显然 if 块和 spaceLengte 变量并不是真正需要的。但我认为这会让 OP 更容易阅读。

祝你好运/成功 ;)