另外你也可以自己写方法,
public static void main (String[] args ){
int m=3//初始值
int n=3//次方数
int result=1//存放结果
while(n>0) {
result*=m
n--
}
System.out.println(result)
}
希望能够帮助你,谢谢
public class Test {public static void main(String[] args){
double m = 2
double n = 3
//使用API,Math.pow(double m,double n) -->>'m' 的 'n' 次方
System.out.println("使用API:" + Math.pow(m, n))
//通过两种循环实现的 'm' 的 'n' 次方
System.out.println("使用while实现:" + MToThePowerOfNByWhile(m,n))
System.out.println("使用for实现:" + MToThePowerOfNByFor(m,n))
}
public static double MToThePowerOfNByWhile(double m,double n)
{
double result = 1
while(n >0)
{
result *= m
n--
}
return result
}
public static double MToThePowerOfNByFor(double m,double n)
{
double result = 1
for(int i = 0i<ni++)
{
result *= m
}
return result
}
}