简体   繁体   English

如何在方法中打印结果?

[英]How do I print out the result in a method?

I need to print out the result of the method power2.我需要打印出 power2 方法的结果。 How do I put it in the variable long a?我如何把它放在变量 long a 中? There is an error at the part long a = power2(d,n); 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;
}



}

as easy as this:就这么简单:

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