using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int n = 0; Console.Write("输入倒三角形的行数,行数的取值3-21之间:"); try { n = int.Parse(Console.ReadLine()); } catch { } if (n < 3 || n > 21) { Console.WriteLine("非法行数!"); return; } for (int i = n; i >= 1; i--) { Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1)); } Console.ReadKey(); } } }
Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1)); 这句代码高高深了,有些看不懂
方法好多,怕你不懂 using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int n = 0; Console.Write("输入倒三角形的行数,行数的取值3-21之间:"); try { n = int.Parse(Console.ReadLine()); } catch { } if (n >= 3 && n <= 21) Console.WriteLine(string.Join("\r\n", Enumerable.Range(1, n).Select(x => new string(' ', x) + new string('*', (n - x) * 2 + 1)).ToArray())); else Console.WriteLine("非法行数!"); } } }
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n = 0;
Console.Write("输入倒三角形的行数,行数的取值3-21之间:");
try
{
n = int.Parse(Console.ReadLine());
}
catch { }
if (n < 3 || n > 21)
{
Console.WriteLine("非法行数!");
return;
}
for (int i = n; i >= 1; i--)
{
Console.WriteLine(new string(' ', n - i + 1) + new string('*', i * 2 - 1));
}
Console.ReadKey();
}
}
}
下面是菜鸟的代码for(int row=1;row<=rowmax;row++)
{
w(g(row,rowmax));
}public string g(int rownum, int rowmax)
{ string star=string.Empty;
int starnum= (rownum*2-1);
star=star+ds(" ",(rowmax-rownum)/2);
star=star+ds("*",starnum);
star=star+ds(" ",(rowmax-rownum)/2);
return star;
}
public static string ds(string src,int num)
{
for(int i=0;i<num;i++)
{
src++;
}
return src;
}public static void w(string str)
{
Console.WriteLine(str);
}
这句代码高高深了,有些看不懂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n = 0;
Console.Write("输入倒三角形的行数,行数的取值3-21之间:");
try
{
n = int.Parse(Console.ReadLine());
}
catch { }
if (n >= 3 && n <= 21)
Console.WriteLine(string.Join("\r\n", Enumerable.Range(1, n).Select(x => new string(' ', x) + new string('*', (n - x) * 2 + 1)).ToArray()));
else
Console.WriteLine("非法行数!");
}
}
}
都是一回事。