我需要打印出power2方法的结果。如何将它放在变量long a中? long a = power2(d,n);
部分出现错误public class homework {
public static void main(String[] args) {
long starttime = System.nanoTime();
long a = power2(d , n);
long t = System.nanoTime() - starttime;
System.out.println("result1():" + a + ": " + t + "ns");
}
public static double power2(double d, long n) {
double r = 1;
while(n != 0) {
if(n%2 == 1) {
r*=d;
}
n = n >> 1;
d = d * d;
}
return r;
}
}
答案 0 :(得分:-1)
如此简单:
public class homework {
public static void main(String[] args) {
long starttime = System.nanoTime();
long a = power2(TheDoubleVariable, The LongVariable);
long t = System.nanoTime() - starttime;
System.out.println("result1():" + a + ": " + t + "ns");
}
public static double power2(double d, long n) {
double r = 1;
while(n != 0) {
if(n%2 == 1) {
r*=d;
}
n = n >> 1;
d = d * d;
}
return r;
}
}