首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教为什么简单计算器,无法实现数字相加

2012-05-16 
请问为什么简单计算器,无法实现数字相加?C# code class Program{static void Main(string[] args){Console

请问为什么简单计算器,无法实现数字相加?

C# code
 class Program    {        static void Main(string[] args)        {            Console.WriteLine("please input a string");            string a = Convert.ToString(Console.ReadLine());            string b = Convert.ToString(Console.ReadLine());            int i = 0;          try           {                    Console.WriteLine("a+b= {0} + {1}={2}", a, b, a+b);                }                catch (FormatException)                {                    Console.WriteLine("输入不是整数");                }                catch (DivideByZeroException)                {                    Console.WriteLine("捕获到了除错误");                }                Console.WriteLine("继续");                Console.ReadKey();            }        }


[解决办法]
你的a和b都是字符串,改成整数

int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());

热点排行