當前位置:編程學習大全網 - 編程軟體 - C#編寫的沙漏圖形

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; } } }

  • 上一篇:C語言裏的符號常量是什麽?
  • 下一篇:虛幻4引擎支持哪些編程語言?
  • copyright 2024編程學習大全網