我无法弄清楚减量运算符(e--)在下面的代码中是如何工作的,所以我在它下面编写了另一个类以获得相同的结果。我想知道递减运算符如何在 Power 类中实现该结果。- 新手。
int result, e;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
System.out.println("2 to the " + i +
" power is " + result);
}
为达到相同结果而编写的代码
int result = 1;
for(int i=0; i < 10; i++) {
if (i > 0) {
result*=2;
}
System.out.println("2 to the " + i +
" power is " + result);
}