【问题标题】:Decrement on While loops减少 While 循环
【发布时间】:2016-06-11 19:43:50
【问题描述】:
// Compute integer powers of 2.
class Power {
  public static void main(String args[]) {
    int e;
    int result;

    for (int i = 0; i < 10; i++) {
      result = 1;
      e = i;

      while(e > 0) {
        result *= 2;
        e--;          // What is supposed to be decrementing and how???
      }

      System.out.println("2 to the " + i + 
                    " power is " + result);
    }
  }
}    

为什么while循环有递减,但运行程序时给人的印象却是递增。这让我很困惑,请有人解释一下这个 While 循环是如何操作的?

C:\Users\enrique\Desktop\Hello.java>java 电源
2 的 0 次方是 1
2 的 1 次方是 2
2 的 2 次方是 4
2 的 3 次方是 8
2 的 4 次方是 16
2 的 5 次方是 32
2 的 6 次方是 64
2 的 7 次方是 128
2 的 8 次方是 256
2的9次方是512

【问题讨论】:

  • 哦,好吧,抱歉。这是我的第一次。谢谢!
  • 您正在打印 i ,它在每次迭代中递增。 e 只是控制 2 应该相乘的次数
  • 谢谢,我会尝试您发布的不同替代方案。
  • 为什么不简单地result = 1 &lt;&lt; i;?或者,为什么不在循环外初始化 result = 1,在 print 语句之前什么都不做,然后加倍呢?

标签: java loops math


【解决方案1】:

e 是指数,while 循环将数字的值加倍,直到指数小于或等于 0。

除非递减,否则它将保持i 的值。

你也可以像这样将它转换成一个for循环

for (int e = i; e > 0; e--) { 
    result *= 2;
} 

我知道这是否是一个学习练习,但 Math.pow 函数可以做同样的事情。

【讨论】:

  • 是说while循环从9(向后)开始循环并递减?同时也是平方(二的幂)不加倍。感谢您的回答。
  • 只有 9 的幂,是的
  • 而且,不,这个数字翻了一番。你乘以 2
  • 这是一种实践,我已经了解基本的 while 循环,但这个循环的行为与我所遇到的非常不同。
  • while 循环的概念没有改变。循环直到条件不成立...
【解决方案2】:

您也可以对其他 int j 执行相同操作,它采用增量步骤计算 i-th 的幂 2

for(int i=0; i<10; i++){
    result = 1;

    for(int j=0; j<i; j++){
        result *= 2;
    }
}

或者,如果你想使用while 内循环:

int j = 0;
while(j < i){
    result *= 2;
    j++;
}

【讨论】:

  • 我知道还有其他方法,但作为一个新手,我想先了解这个:)
  • 在您的代码中,正如 cricket_007 解释的那样,e 控制您的 while 循环针对 i 的每个值运行的次数,以计算 2 的指数 :)
  • 谢谢,我会尝试您发布的不同替代方案。
【解决方案3】:

如果您担心 while 循环,您可以使用以下简化/高效的程序。

int max=10, result=1, two=2;
for(int i=0; i<=max;i++)
{
 System.out.println(two + " pow " + i + " = " + result);
result*=two;
}

【讨论】:

  • 谢谢,我会尝试您发布的不同替代方案。
【解决方案4】:

您在问题中的代码说明

Loop i from 0 to n
  Set result =1 
   Loop from 1 <= i
      result = result * 2 // multiply 2 the number of times equal to i
  Print result
End loop

【讨论】:

  • 好吧,我理解你的解释,剩下的是,如何用减量运算符递增 e?为什么减量运算符似乎增加了变量?
【解决方案5】:
class Power {
public static void main(String args[]) {
  int e;
  int result;

 for(int i=0; i < 10; i++) {
      result =1 ;
      e = i;

      while(e > 0) {
          System.out.println(e); // not part of the original program 
          result *= 2 ;            
          e--;
          System.out.println(e); // not part of the original program           
      }

      // System.out.println("2 to the " + i +             
                      //" power is " + result);      

  }
 }
} 

我发现通过放置System.out.println(e),我可以“看到”变量“e”的行为,以便理解递减。这是输出:

C:\Users\enrique\Desktop\Hello.java>java Power: 1, 0, 2, 1, 1, 0, 3

e = 1(iteration 1), 2^1, e (1) decremented to 0, e = 2 (iteration 2), 2^2, e(2) decremented to 1, e = 1 re-enter The while but is ignored as 2^1 is already registered, e (1) decremented to 0, e = 3 (iteration 3), 2^3…

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多