求助关于ReadLine
using System;using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
double R = Console.ReadLine();
double Area = R * R * PI;
double longth = 2 * R * PI;
Console.WriteLine("{0},{1}", Area, longth);
}
}
}
为什么 这个小程序中, double R=Console..ReadLine()会报错呢?
我这样写没问题呀??? 小弟是初学C#,希望大虾们多多指导 错误信息: 无法将string类型隐式转换为Double型。
把 double R = Console.ReadLine();
改为:double R =Convert.ToDouble( Console.ReadLine()); Console类的ReadLine()方法返回的是一个字符串类型,需要把它强制转换成double类型 ReadLine()方法已经将接受到的数据自动转换为string类型,double和string不能隐式转化。 同意4楼的`` using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("please enter R:");
const double PI = 3.14;
double R = Convert.ToDouble(Console.ReadLine());
double Area = R * R * PI;
double longth = 2 * R * PI;
Console.WriteLine("Area is :{0}\nLongth is :{1}", Area, longth);
Console.ReadKey();
}
}
} Console.ReadLine()读入的是字符串
页:
[1]
