在Java中使用Math.pow来获取电源

Get power using Math.pow in Java

为了获得Java中数字的功效,我们使用java.lang.Math.pow()方法。 Math.pow(double a,double b)方法接受double数据类型的两个参数,并将第一个参数的值提高为第二个参数的幂。

声明-java.lang.Math.pow()方法声明如下-

1
public static double pow(double a, double b)

其中a是基数,b是基数的幂。

让我们看一个程序,使用Math.pow()方法找到数字的幂

现场演示

1
2
3
4
5
6
7
8
9
10
11
import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing some double values
      double x = 5.0;
      double y = 3.0;
      // computing the powers
      System.out.println( x +" raised to the power of" + y +" is" + Math.pow(x,y));
      System.out.println( y +" raised to the power of" + x +" is" + Math.pow(y,x));
   }
}

输出量

1
2
5.0 raised to the power of 3.0 is 125.0
3.0 raised to the power of 5.0 is 243.0