窗体应用程序编写计算器

while (true)
            {
                int sum = 0;
                Console.WriteLine("请输入第一个数字:");
                string num1 = Console.ReadLine();
                while (true)
                {
                    if (!int.TryParse(num1, out sum))
                    {
                        Console.WriteLine("输入的格式不正确,请重新输入!");
                        num1 = Console.ReadLine();
                    }
                    else
                    {
                        num1 = sum.ToString();
                        break;
                    }
                }
                Console.WriteLine("请输入第二个数:");
                string num2 = Console.ReadLine();
                while (true)
                {
                    if (!int.TryParse(num2, out sum))
                    {
                        Console.WriteLine("输入的格式不正确,请重新输入!");
                        num2 = Console.ReadLine();
                    }
                    else
                    {
                        num2 = sum.ToString();
                        break;
                    }
                }
                Console.WriteLine("请选择运算符:1.+ 2.- 3.* 4./");
                string c = Console.ReadLine();
                ADD(int.Parse(num1), int.Parse(num2), c);
                //Console.ReadLine();
            }

        }
        //a=数字1,b=数字2,c=运算符
        static void ADD(int a,int b,string c)
        {
            switch (c)
            {
                case "1":
                    Console.WriteLine("计算结果为:" + (a + b));
                    break;
                case "2":
                    Console.WriteLine("计算结果为:" + (a - b));
                    break;
                case "3":
                    Console.WriteLine("计算结果为:" + (a * b));
                    break;
                case "4":
                    Console.WriteLine("计算结果为:" + (a / b));
                    break;
                default:
                    Console.WriteLine("您输入的运算格式不正确,请重新输入!");
                    break;
            }
        }

你可能感兴趣的:(C#)