java计算 0.5 的 10次方

来源:百度知道 编辑:UC知道 时间:2024/06/01 00:11:40
大哥们救急啊!
急需答案!
要用循环模式计算 详细点

public class PrintPower{
public static void main(String[] args) {
System.out.println(Math.pow(0.5,10));
}
}

public class PrintPower{
public static int i=0;

public static void main(String[] args){
double p=0.5;
power(p);

}
public static double power(double s){
double result=1.0;
i++;
if(i<=10){

result = power(s)*s;
System.out.println(result);
return result;

}
else return result;
}
}

可以运行,以经调试过了,试看好用不。

public static void main(String[] args) {
double num = 0.5;
double ans = 1;
for (int i = 0; i < 10; i++) {
ans *= num;
}
System.out.println(ans);
}

Math.pow很好用的方法:没有特殊要求可以考虑用它
private static double getPow(double pow,int n){//递归