C#编写的沙漏图形

2020-10-28 教育 84阅读
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; } } }
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com