Friday, 27 September 2013

having a powers of 2 questions

having a powers of 2 questions

import acm.program.*;
public class Practice3 extends ConsoleProgram
{
public static int powersOf2(int k){
int x=0;
while (k < 1000){
x=k;
k*=2;
}
return x;
}
public void run()
{
println(powersOf2(1));
println(powersOf2(0));
println(powersOf2(2));
println(powersOf2(-1));
println(powersOf2(3000));
}
}
I don't think I really get right values from powersOf2. only 512 is
displayed when I run program. and if I run it by each println. it gives me
512 none 512 none 0
Is there something wrong? or values are correct?

No comments:

Post a Comment