无法将类型“double”隐式转换为“int”。 -错误

2023-11-29

以下是尝试编写 C# 代码来求圆的面积

using System;
namespace DataTypeApplication 
{
    class Program 
    {
        static void Main(string[] args) 
        {
            double area;
            const double pi = 3.14;
            int side;
            Console.WriteLine("enter the radius of circle:");
            side = Convert.ToDouble(Console.ReadLine());
            area = (pi * side * side);
            Console.WriteLine("area is {}", area);
}
}
}

它给出了一个错误

side = Convert.ToDouble(Console.ReadLine());

其中说

无法将类型“double”隐式转换为“int”。存在显式转换(您是否缺少强制转换?)

我做错了什么?


如果您加入您的声明和转让side多变的

int side = Convert.ToDouble(Console.ReadLine());  

很容易看出您分配了 type 的值double到类型变量int

考虑使用Int32.TryParse安全解析整数或声明的字符串表示的方法side as double.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将类型“double”隐式转换为“int”。 -错误 的相关文章

随机推荐