这是我的作业,我需要创建一个通用的方法来计算功率。 与Math.pow(x,y)相同; 但是x应该是通用的并扩展Number,y是一个int数。
我只能推到这里。
public static <T extends Number> double power(T x, int y){
double result = 0;
for(int i=0; i<y; i++){
}
return result;
}
答案 0 :(得分:1)
这是作业,但我看到你已经写了一些东西。这是我的版本(警告:我的java生锈了)。
public static <T extends Number> double power(T x, int y){
/* I don't feel like calling this in a loop */
double dx = x.doubleValue();
double result = 1;
for(int i=0; i<y; i++){
result *= dx;
}
return result;
}