C#控制台应用程序,无法将类型'double'隐式转换为'string'吗?

时间:2019-03-11 02:39:37

标签: c# error-handling console-application

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

该代码未完成(并在下面发布)。我在Automobile类中的每个吸气剂和吸气剂上都遇到了一些错误。

例如,出现错误“无法将类型'double'隐式转换为'string'”。在读取return make的代码行和make = 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();
        }
    }
}

提前谢谢!

1 个答案:

答案 0 :(得分:2)

创建实例变量private double make时,您告诉程序make的类型为double。在SetModel(string paramMake)中,输入一个字符串,然后尝试将其设置为double的值。字符串是一系列字符,而double是浮点值。编译器本身无法转换它们,这就是给您带来错误的原因(类似的错误出现在代码的其他位置)。

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

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