using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Int32 a = 3;
Int32 b = 5;
a = Console.Read();
b = Convert.ToInt32(Console.ReadLine());
Int32 a_plus_b = a + b;
Console.WriteLine("a + b =" + a_plus_b.ToString());
}
}
}我在ReadLine()函数中得到一条错误消息:
FormatException未被处理。
有什么问题吗?
发布于 2013-08-21 10:29:29
我想这仅仅是因为在输入第一个数字之后按ENTER键。让我们分析你的代码。您的代码读取您输入到a变量的Read()函数所做的第一个符号。但是,当您按enter键时,ReadLine()函数返回空字符串,将其转换为整数是不正确的格式。
我建议您使用ReadLine()函数来读取两个变量。所以输入应该是7->[enter]->5->[enter]。然后得到a + b = 12作为结果。
static void Main(string[] args)
{
Int32 a = 3;
Int32 b = 5;
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Int32 a_plus_b = a + b;
Console.WriteLine("a + b =" + a_plus_b.ToString());
}发布于 2013-08-21 10:18:16
我觉得你应该把:
b = Convert.ToInt32(Console.ReadLine());
在试着抓住块中。
祝好运。
发布于 2013-08-21 10:22:00
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Int32 a = Convert.ToInt32(Console.ReadLine());
Int32 b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("a + b = {0}", a + b);
}
}}
https://stackoverflow.com/questions/18354660
复制相似问题