2016-11-13 54 views
-3

任何人都可以告訴我爲什麼第二個陳述不打印時,計數低於4?第一部分打印「循環」部分,但不打印「不再循環」。怎麼了?雖然循環不打印第二個命令

enter code here 

public class Scratchpad { 

public static void main(String[] args) { 

    int xRay = 7; 

    while (xRay > 4) { 
     System.out.println("looping"); 

     if (xRay < 4) 
      System.out.println("No more loops"); 
     xRay = xRay - 1; 

    } 

} 
+1

請格式化你的代碼。 –

+1

因爲您的循環在xRay小於5時結束 – Quagaar

+0

將xRay的遞減量移到if上方並將條件更改爲xRay <= 4 – Quagaar

回答

1

xRay當到達值4,while循環結束。這就是爲什麼第二個陳述沒有被打印。

如果你想要得到它打印一個解決辦法是這樣:

public class Scratchpad { 

    public static void main(String[] args) { 

     int xRay = 7; 

     while (xRay >= 4){ 
      System.out.println("looping"); 

      if(xRay <= 4) 
       System.out.println("No more loops"); 

      xRay = xRay-1; 

     } 

    } 

}