码迷,mamicode.com
首页 > 其他好文 > 详细

控制台计算器

时间:2017-09-27 14:54:30      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:入职   运算   进入   rgs   cas   exit   错误   oid   return   

一直在改bug,刚入职就是悲催,昨天一个特特新的手问的,随手贴出来吧
    class Program
    {
        static void Main(string[] args)
        {
            int n = 0;
            while (true)
            {
                
                string  n1, n2,op;
                if (n != 0)
                {
                    Console.WriteLine("请用户选择是否进入下一轮运算,按Q键退出");
                }
                Console.WriteLine("请输入第一个数:");
                n1 = Console.ReadLine();
                if (n1.ToString().ToUpper() == "Q")
                {
                    Environment.Exit(0);
                }
                Console.WriteLine("请输入运算符:");
                op = Console.ReadLine();
                Console.WriteLine("请输入第二个数:");
                n2 = Console.ReadLine();
                try
                {
                    string result = s1(n1, op, n2);
                    if (result == "false")
                    {
                        Console.WriteLine("错误:");///计算错误
                    }
                    else
                    {
                        Console.WriteLine(result);
                        n = 1;
                    }
                }
                catch
                {
                    Console.WriteLine("错误112:");///异常错误
                }
                finally
                {

                }
                }
              
            }
        public static string s1(string num1, string opt, string num2)
        {
            switch (opt)
            {
                case "+":
                    return (Convert.ToDouble(num1) + Convert.ToDouble(num2)).ToString();
                case "-":
                    return (Convert.ToDouble(num1) - Convert.ToDouble(num2)).ToString();
                case "*":
                    return (Convert.ToDouble(num1) * Convert.ToDouble(num2)).ToString();
                case "/":
                    return (Convert.ToDouble(num1) / Convert.ToDouble(num2)).ToString();
                default: return "false";

            }
        }
    }
        
    
}

控制台计算器

标签:入职   运算   进入   rgs   cas   exit   错误   oid   return   

原文地址:http://www.cnblogs.com/net-syc/p/7601672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!