2016-07-07 68 views
0
// Setup dummy array 
    ArrayList<Integer> list = dateArray; 
    int counter = 1; 
    while (list.size() != 0) { 
     for (int j = 1; j < list.size(); j++) 
     { 

      //System.out.println(list.get(0) + " and " + list.get(j)); 
      int difference = list.get(0) - list.get(j); 
      if (difference <6){ 
       System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size());  
       counter= counter +1; 
       System.out.println ("Counter is " + counter); 
       if (counter >= 4){ 
        System.out.println ("j = " + j + " Counter =" + counter); 
        if (j ==list.size()-1) { 
         System.out.println ("here " + counter); 
         break; 
        } 
       } 
      } 
     } 
     list.remove(0); 
    }; 

輸出:如何在java中跳出一個while循環?

1 and 2 and size is 4 
Counter is 2 
1 and 3 and size is 4 
Counter is 3 
1 and 4 and size is 4 
Counter is 4 
here 4 
2 and 3 and size is 3 
Counter is 5 
3 and 4 and size is 2 
Counter is 6 
here 6 

理想的情況下,我希望它停止時將計數器4,不繼續執行「2和3的尺寸爲3」

非常感謝!

+1

只是刪除了'while'循環,你想要做的只是執行一次'for'循環。你能否澄清一下,如果你的名單大小不是4,會發生什麼? – jhamon

+0

您可以使用兩個'break'語句。首先把你帶出'for',其次將帶你離開'while'。當然,如果你通過達到條件終止'while循環會更好。 – PatNowak

回答

2

您需要爲循環使用標籤。請嘗試以下片段:

// Setup dummy array 
    ArrayList<Integer> list = dateArray; 
    int counter = 1; 
outerwhileloop: 
    while (list.size() != 0) { 
     for (int j = 1; j < list.size(); j++) 
     { 

      //System.out.println(list.get(0) + " and " + list.get(j)); 
      int difference = list.get(0) - list.get(j); 
      if (difference <6){ 
       System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size());  
       counter= counter +1; 
       System.out.println ("Counter is " + counter); 
       if (counter >= 4){ 
        System.out.println ("j = " + j + " Counter =" + counter); 
        if (j ==list.size()-1) { 
         System.out.println ("here " + counter); 
         break outerwhileloop; 
        } 
       } 
      } 
     } 
     list.remove(0); 
    };  
0

您可以添加一個布爾變量是這樣的:

// Setup dummy array 
ArrayList<Integer> list = dateArray; 
int counter = 1; 
boolean isBreak = false; 
while (list.size() != 0 && !isBreak) { 
    for (int j = 1; j < list.size(); j++) 
    { 

     //System.out.println(list.get(0) + " and " + list.get(j)); 
     int difference = list.get(0) - list.get(j); 
     if (difference <6){ 
      System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size());  
      counter= counter +1; 
      System.out.println ("Counter is " + counter); 
      if (counter >= 4){ 
       System.out.println ("j = " + j + " Counter =" + counter); 
       if (j ==list.size()-1) { 
        System.out.println ("here " + counter); 

        isBreak = true; 
        break; 
       } 
      } 
     } 
    } 
    list.remove(0); 
}; 
1

只需將代碼移動到方法和調用返回。否則,你可以使用一種叫labeled break的東西。

search: 
    for (i = 0; i < arrayOfInts.length; i++) { 
     for (j = 0; j < arrayOfInts[i].length; 
      j++) { 
      if (arrayOfInts[i][j] == searchfor) { 
       foundIt = true; 
       break search; 
      } 
     } 
    } 
1

您可以使用Java標籤,作爲:

int x = 1; 
    outerLoopLabel: 
    while(x != 10) { 
     int fact = 0;  
     for(int i=0;i <=x; i++) {   
     fact +=i; 
     if(fact > 25) { 
      break outerLoopLabel;  
     } 
     } 
     System.out.println("Number: "+ x + " and fact: " + fact);  
     ++x;  
    } 

輸出無標籤:

Number: 1 and fact: 1 
Number: 2 and fact: 3 
Number: 3 and fact: 6 
Number: 4 and fact: 10 
Number: 5 and fact: 15 
Number: 6 and fact: 21 
Number: 7 and fact: 28 
Number: 8 and fact: 36 
Number: 9 and fact: 45 

與標籤輸出:

Number: 1 and fact: 1 
Number: 2 and fact: 3 
Number: 3 and fact: 6 
Number: 4 and fact: 10 
Number: 5 and fact: 15 
Number: 6 and fact: 21