如何打印1,2 2,3 3 3,4 4 4 4 ...通过c#
答案 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());
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());
答案 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("");
}