C#编写的沙漏图形
using System; namespace FunnelTest { class FunnelMain { [STAThread] static void Main(string[] args) { int Par = 8; try { Funnel MyFunnel= new Funnel(Par); MyFunnel.funnelDraw(); Console.WriteLine("{0}! = {1};",Par,MyFunnel.funnelFactorial()); } catch (System.FormatException e) { Console.WriteLine(e.Message.ToString()); } Console.Read(); } } public class Funnel { private int num; public Funnel(int _num) { if(_num < 5 || _num > 15) { throw new System.FormatException("输入的参数必须在5到15之间!对象不能初始化。"); } else num = _num; } public Funnel() { throw new System.FormatException("参数不能为空。"); } public void funnelDraw() { int temp = num; while(temp > 1) { for(int j=temp;j>0;j--) { Console.Write("*"); } Console.Write("\n"); temp--; } while(temp <= num) { for(int j=temp;j>0;j--) { Console.Write("*"); } Console.Write("\n"); temp++; } } public int funnelFactorial() { int Fac = 1; for(int i=1;i<=num;i++) { Fac *= i; } return Fac; } } }