"Displaying Triangles" C# 教科书练习介绍

"Displaying Triangles" Intro to C# textbook exercise

我发现:说到逻辑,我是个白痴。 Here's an imgur upload of problem I am ridiculously stuck on.

我并不是在思考解决方案的逻辑。我看到我打算使用 For 循环来显示以 1 开头并以 10 结尾的星号行(至少对于第一部分),但我不知道如何增加字符串输出本身。

我想创建一个变量,其中循环的值乘以字符串,希望它会显示 loopCounter * asteriskString,但这不可能。

感谢任何帮助。我的逻辑能力需要提高。

可能不是最干净的解决方案,但这会得到你的结果。通读它并弄清楚如何让它变得更好。

using System;    

namespace ConsoleApplication1
{    
   class Program
{        
    static int triangles = 4;
    static int lines = 10;

    static void Main(string[] args)
    {           
        for (int i = 1; i <= triangles; i++)
        {
            if (i==1||i==4)
            {
                for (int j = 1; j <= lines; j++)                    
                    writeAstrix(i, j);                    
            }
            else
            {
                for (int j = lines; j > 0; j--)                    
                    writeAstrix(i, j);
            }              
            Console.WriteLine();
        }

        Console.ReadLine();
    }

    private static void writeAstrix(int i, int j)
    {           
        if (i == 3||i==4)
            makeSpacers(j);

        while (j > 0)
        {
            Console.Write('*');
            j--;
        }
        Console.WriteLine();
    }

    private static void makeSpacers(int iterate)
    {
        int spacers = lines - iterate;
        while (spacers > 0)
        {
            Console.Write(' ');
            spacers--;
        }
    }
 }    
}