要求:1.行数取3--21之间,对非法行数抛出"非法行数";
2. 在控制台上打印出这个行数的倒三角形;如图
实现代码:
static
void
Main(
string
[] args)
{
int intNumber;
Console.Write( " 请输入显示的行数: " );
string number = Console.ReadLine();
bool IsNum = int .TryParse(number, out intNumber);
if ( ! IsNum)
{
Console.Write( " 请不要输入非法数字! " );
}
else if (intNumber < 3 || intNumber > 21 )
{
Console.Write( " 输入的数据最小为3,最大为21! " );
}
else
{
for ( int i = intNumber; i > 0 ; i -- )
{
// 循环空格
for ( int j = 0 ; j < intNumber - i; j ++ )
{
Console.Write( " " );
}
// 循环图形
for ( int k = 2 * i - 1 ; k > 0 ; k -- )
{
Console.Write( " * " );
}
Console.Write( " \n " );
}
}
Console.ReadKey();
}
{
int intNumber;
Console.Write( " 请输入显示的行数: " );
string number = Console.ReadLine();
bool IsNum = int .TryParse(number, out intNumber);
if ( ! IsNum)
{
Console.Write( " 请不要输入非法数字! " );
}
else if (intNumber < 3 || intNumber > 21 )
{
Console.Write( " 输入的数据最小为3,最大为21! " );
}
else
{
for ( int i = intNumber; i > 0 ; i -- )
{
// 循环空格
for ( int j = 0 ; j < intNumber - i; j ++ )
{
Console.Write( " " );
}
// 循环图形
for ( int k = 2 * i - 1 ; k > 0 ; k -- )
{
Console.Write( " * " );
}
Console.Write( " \n " );
}
}
Console.ReadKey();
}