c# - 使用 Math.Pow 函数

标签 c# pow

<分区>

我有一个构造函数,它返回两个带有“to the power of”运算符的整数的结果。我正在尝试使用 math.pow 方法来执行此操作,但我不确定它是如何工作的。

这是我的构造函数

public int Power (int firstNumber, int secondNumber)
{
    result = Math.Pow(firstNumber, secondNumber);
    return result;
}

以及调用它的代码

case "^":
    result = application.Power(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;

在这种情况下如何使用 Math.Pow 方法?

最佳答案

就您遇到的错误而言:

Math.Pow 将 2 个 double 作为参数并返回一个 double 。您的方法被声明为返回一个 int。这意味着 result 是一个整数。您需要将 Math.Pow 的结果转换为 int:

result = (int)Math.Pow(firstNumber, secondNumber);

关于c# - 使用 Math.Pow 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20277010/

相关文章:

C 程序显示数组中的数字(如果是 2 的幂)

c# - Selenium Webdriver 不尊重 cookie 或缓存图像

c# - 在 C# 中使用密码打开 EXCEL (.xlsx)

c# - 为什么是-2?为什么不是-3 或-1?

c# - 创建并打开word文档并将其置于最前面

c - c中的pow数字错误

c# - 如何更改小数格式

c - 替换极其缓慢的 pow() 函数

Javascript : find regexp with capture to replace a^b by Math. pow(a, b)

c++ - 仅使用加法的 pow 函数?