错误1无法隐式转换类型' double' to' int'。存在显式转换(您是否错过了演员?)

时间:2017-02-25 14:54:42

标签: c# type-conversion

我说这个C#代码有错误"错误1无法隐式转换类型' double' to' int'。存在显式转换(您是否错过了演员?)" 错误在哪里?

        Console.Write("Input money : ");
        float money;
        int tax;
        if (money < 10000)
        {
            tax = .05 * money;
        }
        else if (money <= 100000)
        {
            tax = .08 * money;
        }
        else
        {
            tax = .085 * money;
        }

        Console.WriteLine("Tax is {0}", tax);
        Console.ReadLine();
    }
}

2 个答案:

答案 0 :(得分:1)

将您的可变税改为双倍

  double tax = 0;

工作守则,

Console.Write("Input money : ");
float money = Console.Read();          
double tax = 0;
if (money < 10000)
{
    tax = .05 * money;
}
else if (money <= 100000)
{
    tax = .08 * money;
}
else
{
    tax = .085 * money;
}

Console.WriteLine("Tax is {0}", tax);
Console.ReadLine();

答案 1 :(得分:1)

taxint,您尝试为其分配float值。

您必须将tax / float / double声明为decimal或以某种方式对其值进行四舍五入。

编辑:如果你不初始化money的值,使用Console.ReadLine()并解析其返回值

,这也很奇怪