1.打印三角形
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
public static void Main()
{
Console.Write("input the lines number:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
for (int i = 1; i <= lines; i++)
{
for (int k = 1; k <= lines - i; k++)
{
Console.Write(" ");
}
for (int j = 1; j <= i * 2 - 1; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
public static void Main()
{
Console.Write("input the lines number:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
for (int i = 1; i <= lines; i++)
{
for (int k = 1; k <= lines - i; k++)
{
Console.Write(" ");
}
for (int j = 1; j <= i * 2 - 1; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
}
}
}
2.利用stringbuild实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
public static void Main()
{
Console.Write("input the lines number:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= lines; i++)
{
sb.Append(' ', lines - i);
sb.Append('*', i * 2 - 1);
Console.WriteLine(sb.ToString());
sb.Remove(0, sb.Length);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
public static void Main()
{
Console.Write("input the lines number:");
int lines = int.Parse(Console.ReadLine());
Console.WriteLine("");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= lines; i++)
{
sb.Append(' ', lines - i);
sb.Append('*', i * 2 - 1);
Console.WriteLine(sb.ToString());
sb.Remove(0, sb.Length);
}
}
}
}
做个快乐的自己。
浙公网安备 33010602011771号