joezhoutao 发表于 2008-7-6 09:41

求助关于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()会报错呢?

joezhoutao 发表于 2008-7-6 10:04

我重新查了网上的MSDN library
我这样写没问题呀???

joezhoutao 发表于 2008-7-6 10:10

小弟是初学C#,希望大虾们多多指导

luzeqiang 发表于 2008-7-6 12:22

错误信息: 无法将string类型隐式转换为Double型。
把 double R = Console.ReadLine();
改为:double R =Convert.ToDouble( Console.ReadLine());

xiaoshu838 发表于 2008-7-6 21:49

Console类的ReadLine()方法返回的是一个字符串类型,需要把它强制转换成double类型

xiguaaka 发表于 2008-7-7 09:16

ReadLine()方法已经将接受到的数据自动转换为string类型,double和string不能隐式转化。

xyq701830 发表于 2008-7-8 17:26

同意4楼的``

细雨斜飞 发表于 2008-8-2 21:31

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();

        }
    }
}

karv 发表于 2008-8-2 22:53

Console.ReadLine()读入的是字符串

页: [1]

编程论坛