黑马程序员技术交流社区

标题: 自己写的一个超级简陋的四则运算器,求指教 [打印本页]

作者: 赵贺景    时间: 2014-5-10 16:38
标题: 自己写的一个超级简陋的四则运算器,求指教
        static void Main(string[] args)
        {
            //简单的四则运算
            do
            {            
                try
                    //try catch语句处理异常
                {
                    Console.WriteLine("请输入一个数字");
                    int input1 = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("请再次输入一个数字");
                    int input2 = Int32.Parse(Console.ReadLine());
                    //这里可以两个接取的值可以用方法写在一起,但是不容易看懂
                    Console.WriteLine("请选择要进行的运算,输入+-*/");
                    string s = Console.ReadLine();
                    if (s == "+")
                    {
                        Console.WriteLine("{0}+{1}={2}", input1, input2, Add.add(input1, input2));
                    }
                    else if (s == "-")
                    {
                        Console.WriteLine("{0}-{1}={2}", input1, input2, Less.less(input1, input2));
                    }
                    else if (s == "*")
                    {
                        Console.WriteLine("{0}*{1}={2}", input1, input2, Cheng.cheng(input1, input2));
                    }
                    else if (s == "/")
                    {
                        Console.WriteLine("{0}/{1}={2}", input1, input2, Chu.chu(input1, input2));
                    }
                    else
                    {
                        Console.WriteLine("输入有误,请重新输入");
                    }
                }
                catch
                {
                    Console.WriteLine("输入错误,请重新输入");
                }
            }
            while (true);

        }
        public class Add
        {
            //static 静态修饰符,只能有静态成员访问
            public static int add(int a, int b)
            {
                return (a + b);
            }
        }
        public class Less
        {

            public static int less(int a, int b)
            {
                return (a - b);
            }

        }
        public class Cheng
        {

            public static int cheng(int a, int b)
            {
                return (a * b);
            }
        }
        public static class Chu
        {


            public static double chu(double a, double b)
            {
                return (a / b);
            }
        }
    }
}
准备有时间在慢慢丰富,再用winform写出来



作者: /fendou    时间: 2014-5-10 17:11
运算 参数除了除法 参数都是Int整形
作者: /fendou    时间: 2014-5-10 17:13
不错 以后丰富就行了 可以重载 函数也可以看看泛型
作者: .net_交流    时间: 2014-5-10 19:22
             int input1,input2;
                try
                    //try catch语句处理异常
                {
                    Console.WriteLine("请输入一个数字");
                     input1 = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("请再次输入一个数字");
                     input2 = Int32.Parse(Console.ReadLine());
            }
我认为 try 这部分就够了 ,不需要那么多


作者: 赵贺景    时间: 2014-5-10 19:32
.net_交流 发表于 2014-5-10 19:22
int input1,input2;
                try
                    //try catch语句处理异常

恩  也是,毕竟 报异常 也就类型转换这里
作者: 袁晓俊    时间: 2014-5-10 22:23
写的差不多了啊,,,




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2