我初学C#,以前只学过C。
刚做了下面那个题目,但是给b[i]赋值那行如果将Convert.ToDouble去掉就会出现错误:“无法将类型“string”隐式转换为“double””,加上后就好了,我没用到string类型的变量啊,为什么会出现这种错误呢?
哪位高手能给我解答一下呢,十分感谢!using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TriangleArea
{
class Program
{
static void Main(string[] args)
{
int i=0;
double s, area;
double[] b={0,0,0};
for (i=0; i < 3; i++)
{
Console.WriteLine("请输入三角形的边长{0}",i+1);
//下面这行有点疑问
b[i] = Console.ReadLine();
}
s = 1.0/ 2.0*(b[0] + b[1] + b[2]);
area = Math.Sqrt(s * (s - b[0]) * (s - b[1]) * (s - b[2]));
Console.WriteLine("三边长分别为:{0},{1},{2}的三角形面积为:{3}",b[0],b[1],b[2],area);
Console.ReadLine();
}
}
}
刚做了下面那个题目,但是给b[i]赋值那行如果将Convert.ToDouble去掉就会出现错误:“无法将类型“string”隐式转换为“double””,加上后就好了,我没用到string类型的变量啊,为什么会出现这种错误呢?
哪位高手能给我解答一下呢,十分感谢!using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TriangleArea
{
class Program
{
static void Main(string[] args)
{
int i=0;
double s, area;
double[] b={0,0,0};
for (i=0; i < 3; i++)
{
Console.WriteLine("请输入三角形的边长{0}",i+1);
//下面这行有点疑问
b[i] = Console.ReadLine();
}
s = 1.0/ 2.0*(b[0] + b[1] + b[2]);
area = Math.Sqrt(s * (s - b[0]) * (s - b[1]) * (s - b[2]));
Console.WriteLine("三边长分别为:{0},{1},{2}的三角形面积为:{3}",b[0],b[1],b[2],area);
Console.ReadLine();
}
}
}
你这个读出来的就是一个string类型的数据流!
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/