2016-09-12 89 views
-3

我的代碼:斷點不起作用

class MixFor5 { 
public static void main (String [] args) { 
    int x = 0; 
    int y = 30; 
    for (int outer = 0; outer < 3; outer++) { 
     for (int inner = 4; inner > 1; inner--) { 
      x = x + 3; 
      y = y - 2; 
      if (x == 6) { 
       break; // *Useless break;* 
      } 
      x = x + 3; 
     } 
     y = y - 2; 
    } 
    System.out.println(x + " " + y); 
} 
} 

我的輸出:

有人能向我解釋。爲什麼當我刪除休息時;我的輸出數據根本不會改變。

+2

從來沒有嘗試過使用調試器?這將是完美的情況。 – Tom

+0

打破只打破內部循環。這不是一個斷點! –

回答

1

你永遠履行if(x==6)

讓我們來看看在第一個循環:

int x = 0; 

//.... 

x = x + 3; // x = 3; 
if(x == 6) //false 
    break; 

x = x + 3; // x = 6 

現在第二循環

x = x + 3 // x = 9 

if(x == 6) //false x = 9 
    break; 
x = x + 3; //x = 12 

所以你永遠等於6進行比較時, 。

+0

很酷,謝謝。 –