3

我无法让 4 种不同的三角形图案并排出现。这是一个控制台应用程序。

这正是我试图通过使用嵌套 for 循环来实现的目标:

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

我已经让单独的模式工作了,但当然它们会一个接一个地出现。

using System;

class Assignment5
{
    static void Main()
    {
        for (int i = 1; i <= 10; i++) // Outer loop for number of rows
        {
            for (int j = 1; j <= i; j++) // Inner loop for number of stars
            {
                Console.Write("*");
            }
            Console.WriteLine();
        } // End First Pattern

        for (int i = 10; i >= 1; i--) // Outer loop for number of rows
        {
            for (int j = 1; j <= i; j++) // Inner loop for number of stars
            {
                Console.Write("*");
            }
            Console.WriteLine();
        } // End Second Pattern

        for (int i = 10; i >= 1; i--) // Outer Loop for number of rows
        {
            for (int j = 1; j <= 10 - i; j++)         //Inner loop for number of spaces
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= i; k++)  //Secondary inner loop for number of stars
            {
                Console.Write("*");
            }
            Console.WriteLine();

        } // End Third Pattern

        for (int i = 1; i <= 10; i++)               //Outer Loop for number of rows
        {
            for (int j = 1; j <= 10 - i; j++)         //Inner loop for number of spaces
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= i; k++)  //Secondary inner loop for number of stars
            {
                Console.Write("*");
            }
            Console.WriteLine();
        } // End Fourth Pattern

        Console.WriteLine("Press Enter for Part 2 of this Program");
        Console.ReadKey();
        Console.Clear();

    } // End main function


} // End class Assignment5
4

7 回答 7

3

你没有指定你想如何实现,所以我向你展示

        private static void Main()
        {
            Console.WriteLine(
@"*         ********************         *
**        *********  *********        **
***       ********    ********       ***
****      *******      *******      ****
*****     ******        ******     *****
******    *****          *****    ******
*******   ****            ****   *******
********  ***              ***  ********
********* **                ** *********
***********                  ***********");
        }

[编辑] 好的,这是一个不那么滑稽的答案。;)

int n = 10;

for (int i = 0; i < n; ++i)
{
    for (int j = 0; j <= i; ++j)
        Console.Write("*");

    for (int j = 0; j < n-i-1; ++j)
        Console.Write(" ");

    for (int j = 0; j < n-i; ++j)
        Console.Write("*");

    for (int j = 0; j < 2*i; ++j)
        Console.Write(" ");

    for (int j = 0; j < n-i; ++j)
        Console.Write("*");

    for (int j = 0; j < n-i-1; ++j)
        Console.Write(" ");

    for (int j = 0; j <= i; ++j)
        Console.Write("*");

    Console.WriteLine();
}

[第二次编辑]

n编写输出星号或空格的方法会更具可读性,如下所示:

static void stars(int count)
{
    for (int i = 0; i < count; ++i)
        Console.Write("*");
}

static void spaces(int count)
{
    for (int i = 0; i < count; ++i)
        Console.Write(" ");
}

接着:

int n = 10;

for (int i = 0; i < n; ++i)
{
    stars(i+1);
    spaces(n-i-1);
    stars(n-i+1);
    spaces(2*i);
    stars(n-i);
    spaces(n-i-1);
    stars(i+1);

    Console.WriteLine();
}
于 2013-08-07T07:40:19.557 回答
2

这是给你的脑筋急转弯。

for (int n = 10; n > 0; n--)
{
    var tri = "".PadRight(11 - n, '*').PadRight(10, ' ') + "".PadRight(n, '*').PadRight(10, ' ');
    Console.WriteLine(tri + String.Join("", tri.ToCharArray().Reverse()));
}

输出:

* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ***** ***** ****
***** ****** ****** *****
****** ***** ***** ******
***** **** **** *******
********* *** *** *********
********* ** ** *********
*********** ***********
于 2013-08-07T09:18:12.880 回答
1
using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int length = 10;

            for (int i = 0; i < length; i++)
            {
                string result = String.Format("{0}{1}{2}{3}",
                     fillWithStarFromLeft(i + 1, length),
                     fillWithStarFromLeft(length - i, length),
                     fillWithStarFromRight(length - i, length),
                     fillWithStarFromRight(i + 1, length)
                     );

                Console.WriteLine(result);
            }

            Console.ReadKey();
        }

        private static object fillWithStarFromRight(int length, int segmentlength)
        {
            string result = String.Empty;

            for (int i = 0; i < length; i++)
            {
                result += "*";
            }

            return result.PadLeft(segmentlength, ' ');
        }

        private static string fillWithStarFromLeft(int length, int segmentlength)
        {
            string result = String.Empty;

            for (int i = 0; i < length; i++)
            {
                result += "*";
            }

            return result.PadRight(segmentlength, ' ');
        }
    }
}

只是快速但希望它有所帮助

于 2013-08-07T08:16:40.970 回答
1

这将为您提供所需的结果。

using System.IO;
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 10; i++) // Outer loop for number of rows
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write("*");
            }
            for (int k = 10; k >= i; k--)
            {
                Console.Write(" ");
            }            
            for (int l = 10-i; l >= 0; l--)
            {
                Console.Write("*");
            }
            for (int k = 0; k <= i*2; k++) 
            {
                Console.Write(" ");
            }
            for (int k = 10-i; k >= 0; k--)
            {
                Console.Write("*");
            }
            for (int k = 10; k >= i; k--) 
            {
                Console.Write(" ");
            }
            for (int j = 1; j <= i; j++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        } 

        Console.ReadKey();
        Console.Clear();

    }
}
于 2013-08-07T08:09:51.410 回答
0

另一种解决方案:您创建一个包含所有“*”的数组,然后显示它。

我使用了一个 3 维数组:行、列、模式编号。
我什至将所有内容都移到了 0 < x < 10 中,因为在 C 和 C# 的世界中,数组是从 0 开始的。
请注意,我什至填充了数组的“空白”空间。

static void Main()
{
    var chars = new char[10, 10, 4];

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i >= j)
            {
                chars[i, j, 0] = '*';
            }
            else
            {
                chars[i, j, 0] = ' ';
            }
        }
    } // End First Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i <= 9 - j)
            {
                chars[i, j, 1] = '*';
            }
            else
            {
                chars[i, j, 1] = ' ';
            }
        }
    } // End Second Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i <= j)
            {
                chars[i, j, 2] = '*';
            }
            else
            {
                chars[i, j, 2] = ' ';
            }
        }
    } // End Third Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i >= 9 - j)
            {
                chars[i, j, 3] = '*';
            }
            else
            {
                chars[i, j, 3] = ' ';
            }
        }
    } // End Fourth Pattern

    for (int i = 0; i < 10; i++)
    {
        for (int k = 0; k < 4; k++)
        {
            for (int j = 0; j < 10; j++)
            {
                Console.Write(chars[i, j, k]);
            }
        }

        Console.WriteLine();
    }

    Console.WriteLine("Press Enter for Part 2 of this Program");
    Console.ReadKey();
    Console.Clear();

} // End main function

正如我告诉你的,我在数组创建过程中用空格填充了空格。我本可以跳过它,而是替换 Console.Write 中的空格chars[i, j, k] == 0' '例如:

static void Main()
{
    var chars = new char[10, 10, 4];

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i >= j)
            {
                chars[i, j, 0] = '*';
            }
        }
    } // End First Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i <= 9 - j)
            {
                chars[i, j, 1] = '*';
            }
        }
    } // End Second Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i <= j)
            {
                chars[i, j, 2] = '*';
            }
        }
    } // End Third Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10; j++)
        {
            if (i >= 9 - j)
            {
                chars[i, j, 3] = '*';
            }
        }
    } // End Fourth Pattern

    for (int i = 0; i < 10; i++)
    {
        for (int k = 0; k < 4; k++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (chars[i, j, k] != 0)
                {
                    Console.Write(chars[i, j, k]);
                }
                else
                {
                    Console.Write(' ');
                }
            }
        }

        Console.WriteLine();
    }

    Console.WriteLine("Press Enter for Part 2 of this Program");
    Console.ReadKey();
    Console.Clear();

} // End main function

从那里我们可以删除if并使它们成为 的一部分for,但它变得非常难以阅读......

static void Main()
{
    var chars = new char[10, 10, 4];

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j <= i; j++)
        {
            chars[i, j, 0] = '*';
        }
    } // End First Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 0; j < 10 - i; j++)
        {
            chars[i, j, 1] = '*';
        }
    } // End Second Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = i; j < 10; j++)
        {
            chars[i, j, 2] = '*';
        }
    } // End Third Pattern

    for (int i = 0; i < 10; i++) // Outer loop for number of rows
    {
        for (int j = 9 - i; j < 10; j++)
        {
            chars[i, j, 3] = '*';
        }
    } // End Fourth Pattern

    for (int i = 0; i < 10; i++)
    {
        for (int k = 0; k < 4; k++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (chars[i, j, k] != 0)
                {
                    Console.Write(chars[i, j, k]);
                }
                else
                {
                    Console.Write(' ');
                }
            }
        }

        Console.WriteLine();
    }

    Console.WriteLine("Press Enter for Part 2 of this Program");
    Console.ReadKey();
    Console.Clear();

} // End main function
于 2013-08-07T08:11:25.360 回答
0

试试这个(非常短的版本)

int sz=10;
for (int i=0;i<sz;i++){
    for(int j=0;j<(sz*4);j++){
        if(j<=i || (j>=sz && j<=(sz*2)-(i+1))
        || (j>=sz*2 && j>=(sz*2)+i && j<sz*3)
        || (j>=(sz*4)-(i+1))
        )
        {
           Console.Write("*");
        }
        else {Console.Write(" ");}
    }
    Console.WriteLine();
}

在此处输入(未测试)。但这可能会奏效!

于 2013-08-07T08:40:38.560 回答
0

您正在使用在当前光标位置写入的Console.Writeand命令。Console.WriteLine您的循环始终在 10*10 字符范围内工作,后跟换行符。

解决问题的一种方法是绝对光标位置,将光标设置到正确的位置,然后绘制一个字符。每个后续三角形都必须移动一个偏移量:

int leftOffset = 0;

// draw first triangle
for x in ...
   for y in ...
      Console.SetCursorPosition(x, y);
      Console.Write("*");

leftOffset += 10;

// draw second triangle
for x in ...
   for y in ...
      Console.SetCursorPosition(x + leftOffset, y);
      Console.Write("*");

.
.
.

请务必在开始之前清洁控制台!

于 2013-08-07T07:38:25.207 回答