提问者:小点点

正在学习C#并了解此错误。 无法将类型“double”隐式转换为“int”。 存在显式转换


这是带有错误的代码


共3个答案

匿名用户

Double和int是两个不同变量。 Int是整数,double是小数(最多16个小数)。 您需要将int转换为double,以便在所做的工作中使用它。 试着在它之前推杆(双人)。 double myDouble=(双)myint;

编辑:没有看到您的代码。 如果你乘以1.5(没有f),就会是一个双倍。 你需要做(int)(距离*1.50F)。

匿名用户

我不打算使用这个链接,但下面是如何将双数转换成整数的:

int a;
double b = 1;
b = Convert.ToInt32(a);

匿名用户

使用double类型进行计算,更正了您的代码

    static void Main(string[] args)
    {
        Console.WriteLine("Please enter the distance of journey in Kilometers");
        double distance = Convert.ToDouble(Console.ReadLine());
        double distanceprice = distance * 1.50;
        Console.WriteLine("Please enter the number of passengers");
        double passenger = Convert.ToDouble(Console.ReadLine());
        double passengerprice = passenger * 2;
        double finalprice = passengerprice + distanceprice;
        Console.WriteLine(finalprice);
        Console.ReadLine();
    }