为什么我会收到将类型double隐式转换为int的错误? [英] Why am I getting the error implicitly converting type double to an int?

查看:51
本文介绍了为什么我会收到将类型double隐式转换为int的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修复错误无法将类型'double'隐式转换为'int'.存在显式转换(您是否缺少类型转换?).我看不到将int更改为double的位置.这是我正在使用的以下代码:

How do I fix the error "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). I don't see where I am changing an int to a double. Here's the following code I am working with:

namespace Chapter_9
{
    class Program
    {
        static void Main(string[] args)
        {
            Circle create = new Circle();
            create.SetRadius(2);
            WriteLine("Radius = {0}", create.GetRadius());
            WriteLine("Diameter = {0}", create.GetDiameter());
            WriteLine("Area = {0}", create.GetArea());
        }
    }
    class Circle
    {
        private int Radius;
        private readonly int Diameter;
        private readonly double Area;

        public Circle()
        {
            CircleRadius = 1;
        }
        public int CircleRadius { get; set; }
        public int GetRadius()
        {
            return Radius;
        }
        public void SetRadius(int radius)
        {
            Radius = radius;
        }
        public int GetDiameter()
        {
            int Diameter = Radius * 2;
            return Diameter;
        }
        public int GetArea()
        {
            double Area = Radius * Radius * Math.PI;
            return Area; <--------- !!!!ERROR IS HERE!!!
        }
    }
}

推荐答案

如您所说,您的主要问题是您希望对整数进行精确的十进制计算.但是,您的 public int GetArea()的返回类型是 int .要解决您眼前的问题-只需将其类型更改为double,就可以了!

As you said, your main problem is that you want precise decimal calculation over integers. Yet your return type of public int GetArea() is int. To fix your immediate problem - just change its type to double and that will be it!

但是,您还可以进行其他一些改进.首先-您具有 public int CircleRadius {get;放;} 未使用.然后,就像@Enigmativity所说的那样,您正在用Java风格编写此代码.多使用自动属性,会容易得多.并记下您的字段-您已声明了它们,但尚未使用...

However there are a couple of other improvements you can make. First of all - you have public int CircleRadius { get; set; } that is not used. Then, as @Enigmativity said you are writing this in Java style. Use autoproperties a bit more, it will be much easier. And take note of your fields - you declared them but have not used...

这是一个清理过的课程:

Here is a cleaned up class:

class Circle
{
    public double Radius {get; set;}
    public double Diameter 
    { 
        get 
        {
            return Radius * 2;
        }
    }
    public double Area
    { 
        get 
        {
            return Radius * Radius * Math.PI;
        }
    }

    public Circle()
    {
        this.Radius = 1;
    }
}

您的主要目标是

static void Main(string[] args)
{
    Circle create = new Circle();
    create.Radius = 2;
    WriteLine("Radius = {0}", create.Radius);
    WriteLine("Diameter = {0}", create.Diameter);
    WriteLine("Area = {0}", create.Area);
}

这篇关于为什么我会收到将类型double隐式转换为int的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆