![](/img/trans.png)
[英]How do I have a toString method properly print out the result of 2 mixed fractions?
[英]How do I print out the result in a method?
我需要打印出 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;
}
}
就這么簡單:
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.