簡體   English   中英

如何在方法中打印結果?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM