繁体   English   中英

C# 控制台应用程序,不能将类型“double”隐式转换为“string”?

[英]C# Console application, cannot implicitly convert type 'double' to 'string'?

我超级难倒。 我正在尝试创建一个程序,允许用户创建“汽车”并声明所述汽车的品牌、型号和颜色。

代码未完成(并在下面发布)。 我在汽车类中的每个 getter 和 setter 上都有几个错误。

例如,我收到“无法将类型 'double' 隐式转换为 'string' 的错误。 在读取return makemake = paramMake以及其余的 getter 和 setter 的代码行上。

有任何想法吗?

namespace Program_4cs
{
    class Automobile
    {
        //2.
        //a). PDDC
        public Automobile()
        {
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("a). Automobile PDDC set.");
        }

        //PDC
        public Automobile(string paramMake, string paramModel, string paramColor)
        {
            string make = paramMake;
            string model = paramModel;
            string color = paramColor;
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("a). Automobile PDC set.");
        }

        //b). Getters and Setters
        public string GetMake()
        {
            return make;
        }
        public void SetMake(string paramMake)
        {
            make = paramMake;
        }
        public string GetModel()
        {
            return model;
        }
        public void SetModel(string paramModel)
        {
            model = paramModel;
        }
        public string GetColor()
        {
            return color;
        }
        public void SetColor(string paramColor)
        {
            color = paramColor;
        }

        //c). Speed changing members
        public double GetSpeed()
        {
            return speed;
        }
        public void SetSpeed(double paramSpeed)
        {
            speed = paramSpeed;
        }
        public void IncreaseSpeed(double paramIncreaseSpeedBy)
        {
            speed = speed + paramIncreaseSpeedBy;
        }
        public void DecreaseSpeed(double paramDecreaseSpeedBy)
        {
            speed = speed - paramDecreaseSpeedBy;
        }
        public void ShowSpeed(double paramShowSpeed)
        {
            speed = paramShowSpeed;
        }

        //d). instance members
        private double speed;
        private double make;
        private double model;
        private double color;
    }
    class Program
    {
        static void Main(string[] args)
        {
            //1.
            //Ouput a header in the console
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("1. This is Program-4.");

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Press any key to continue.");

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("How many automobiles would you like to create?");

            Console.ForegroundColor = ConsoleColor.White;
            int numOfAutomobiles = int.Parse(Console.ReadLine());
            Automobile[] listOfAutomobiles = new Automobile[numOfAutomobiles];

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Please input model, and color");

            Console.ForegroundColor = ConsoleColor.White;
            for (int i = 0; i < listOfAutomobiles.Length; i++)
            {
                Console.WriteLine("Please enter the make: ");
                String make = Console.ReadLine();
                Console.WriteLine("Please enter the model: ");
                String model = Console.ReadLine();
                Console.WriteLine("Please enter the color: ");
                string color = Console.ReadLine();
                Automobile newCar = new Automobile(make, model, color);
                listOfAutomobiles[i] = newCar;
            }

            Console.ReadKey();
        }
    }
}

先感谢您!

当您创建实例变量private double make您告诉程序make的类型为double SetModel(string paramMake)您接收一个字符串并尝试将其设置为双SetModel(string paramMake)值。 字符串是一系列字符,而双精度值是浮点值。 编译器不能自己转换这些,这就是给你错误的原因(类似的错误出现在代码的其他地方)。

为了解决这个问题,我们可以将 make 存储为字符串而不是双精度值。 为此,我们将private double make更改为private string make 我们的另一个选择是解析字符串以获得双精度值。 我们可以用Convert.ToDouble()做到这一点。

我认为您应该在这里考虑的一个问题是,double 还是 string 是您使用的最佳选择。 将模型和颜色存储为双精度值似乎可以通过字符串更好地完成,而速度适合双精度值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM