Math.Pow给人"无法隐式转换类型'双'到'浮动'"错误 [英] Math.Pow gives "Cannot implicitly convert type 'double' to 'float' " error

查看:160
本文介绍了Math.Pow给人"无法隐式转换类型'双'到'浮动'"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个节目,我想创建一个简单的计算器。不过,我似乎无法找到一个方法达到 Math.Pow 线时要克服上述错误。

 命名空间BinaryCalc
{
    类二进制
    {
        公共静态无效的主要()
        {        INT加法,减法;
        浮分裂,增殖,电力,开方;        INT X;
        诠释Ÿ;
        X = 10;
        Y = 7;        //Console.WriteLine(\"Please输入对于x)的数;
        //串线=到Console.ReadLine();
        // INT X = int.Parse(线);        //Console.WriteLine(\"Please输入y)的一个号码;
        //字符串LINE2 =到Console.ReadLine();
        // INT Y = int.Parse(2号线);
        此外=(INT)X +(INT)Y;
        减法=(INT)x - (INT)Y;
        师=(浮点)X /(浮动)Y;
        乘法=(浮点)X *(浮动)Y;        功率= Math.Pow(X,2);
        开方=的Math.sqrt(x)的;
        Console.WriteLine(加法结果以{0},补充);
        Console.WriteLine(减法,在{0}减法的结果);
        Console.WriteLine(师的结果{0},除);
        Console.WriteLine(乘法在{0}相乘的结果);
        Console.WriteLine({0}平方在{0}结果,X,功率);
        Console.WriteLine({0}的平方根:{0},X,开方);        }
    }
}


解决方案

Math.Pow使用双击参数。正如错误说,没有的的从转换双浮动,所以转换结果的明确的浮动:

 功率=(浮点)Math.Pow(X,2);

修改结果
修正后的转换顺序

In this program I am trying to create a simple calculator. However, I can't seem to find a way to overcome the aforementioned error when reaching the Math.Pow line.

namespace BinaryCalc
{
    class Binary
    {
        public static void Main()
        {

        int addition,subtraction;
        float division, multiplication, power, sqrt;

        int x;
        int y;
        x = 10;
        y = 7;

        //Console.WriteLine("Please enter a number for x");
        //string line = Console.ReadLine();
        //int x = int.Parse(line);

        //Console.WriteLine("Please enter a number for y");
        //string line2 = Console.ReadLine();
        //int y = int.Parse(line2);


        addition = (int)x + (int)y;
        subtraction = (int)x - (int)y;
        division = (float)x / (float)y;
        multiplication = (float)x * (float)y;

        power = Math.Pow(x,2);
        sqrt = Math.Sqrt(x);


        Console.WriteLine(" Addition results in {0}", addition);
        Console.WriteLine(" Subtraction results in {0}", subtraction);
        Console.WriteLine(" Division results in {0}", division);
        Console.WriteLine(" Multiplication results in {0}", multiplication);
        Console.WriteLine(" {0} squared results in {0}",x, power);
        Console.WriteLine(" Square root of {0} is: {0}", x, sqrt);

        }
    }
}

解决方案

Math.Pow uses a double argument. As the error says, there is no implicit conversion from double to float, so convert the result explicitly to float:

power = (float)Math.Pow(x, 2);

EDIT
corrected the conversion order

这篇关于Math.Pow给人"无法隐式转换类型'双'到'浮动'"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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