第一章 (1)业务与界面相分离

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请输入数字A");
                string numberA = Console.ReadLine();
                Console.WriteLine("请输入运算符");
                string operate = Console.ReadLine();
                Console.WriteLine("请输入数字B");
                string numberB = Console.ReadLine();

                string result = Convert.ToString(GetResult(Convert.ToDouble(numberA), Convert.ToDouble(numberB), operate));
                Console.WriteLine("结果为:"+result);
                
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("输入有误请重新输入");
            }
        }




        /// <summary>
        /// 实现业务逻辑与界面相分离
        /// </summary>
        public static double GetResult(double number1, double number2, string operate)
        {
            double result = 0d;
            switch (operate)
            {
                case "+":
                    result = number1 + number2;
                    break;
                case "-":
                    result = number1 - number2;
                    break;
                case "*":
                    result = number1 * number2;
                    break;
                case "/":
                    if ( number2!=0)
                    {
                        result = number1 / number2;
                    }
                    else
                    {
                        result = -1;
                    }
                    break;
            }
            return result;
        }


    }

 应该把业务方法放到不同的类中,这里为了简单就直接这样写了。

posted on 2013-10-24 08:46  中子持心  阅读(249)  评论(0编辑  收藏  举报

导航