如何打印1,2 2,3 3 3,4 4 4 4 ...通过c#

时间:2011-03-25 13:40:01

标签: c# .net

如何打印1,2 2,3 3 3,4 4 4 4 ...通过c#

3 个答案:

答案 0 :(得分:44)

Console.WriteLine("1,2 2,3 3 3,4 4 4 4...");

答案 1 :(得分:6)

        var builder = new StringBuilder();
        var to = 4;

        for (int i = 1; i <= to; i++)
        {
            for (int count = 0; count <= i; count++)
            {
                builder.AppendFormat("{0} ", i);
            }
            if (i != to)
            {
                builder.Append(String.Format(","));
            }
        }

        builder.Append("...");
        Console.WriteLine(builder.ToString());

enter image description here

 var builder = new StringBuilder();
        var to = 4;

        for (int i = 1; i <= to; i++)
        {
            for (int count = 0; count <= i; count++)
            {
                builder.AppendFormat("{0} ", i);
            }
            if (i != to)
            {
                builder.Append(String.Format("\n"));
            }
        }

        builder.Append("...");
        Console.WriteLine(builder.ToString());

enter image description here

答案 2 :(得分:0)

for (int i = 1; i <= 10; i++)
{

    for (int k = 1; k <= i; k++)
    {
        if (k > 1) Console.Write(" ");
        Console.Write(i);
    }

    Console.Write(",");
}

或换行符而不是逗号:

for (int i = 1; i <= 10; i++)
{

    for (int k = 1; k <= i; k++)
    {
        if (k > 1) Console.Write(" ");
        Console.Write(i);
    }

    Console.WriteLine("");
}