2016-07-19 278 views
-2

我工作的while循环,我想停止,而变量是小于100.与我目前的,我的输出停止在128 - 不知道为什么它打印128当最后一次输出应该是64.任何指针?Java虽然循环

import java.util.Scanner; 

public class InsectGrowth { 
public static void main (String [] args) { 
    int numInsects = 0; 

    numInsects = 8; 

    System.out.print(numInsects + " "); 

    while (numInsects < 100) { 
    numInsects = numInsects * 2; 
    System.out.print(numInsects + " "); 
    } 

    System.out.println(); 

    return; 
    } 
} 
+1

“**停止**而变量小于100”?然后'while(numInsects> = 100)'。 –

+0

你的意思是“我想在变量小于100时运行*”。 – Mfusiki

+1

当'numInsects'是'64'时是否小于'100'?如果这是真的,循环中会发生什么? – azurefrog

回答

0

打印的最后一个值是128,因为你执行

numInsects = numInsects * 2; 

之前

System.out.print(numInsects + " "); 

因此循环停止时numInsects为64,但在打印前通过2相乘,因此为什么你看128.

+0

完美的工作!非常感谢。 – Julia

+0

很高兴帮助:)如果你接受我的答案,这个问题可以关闭。 – Tyler