2011-03-04 35 views
0

我已經寫了下面的代碼,如果這個else if語句運行我想從這個方法突然出來,並回到這個方法被調用的下一行。我已經使用return,但它doesn工作不好。從這個方法出來

else if (balance == 0 && noSolution == 0) { 
       noSolution = 0; 
       return; // it doesn't work. 
      } 

方法:

public <E> void rand_Function(List<E> tree, List<E> array) { 

    if (array.isEmpty()) { 
     return; 
    } 
    if (array.size() == 1) { 
     preorder = (List<Element>) new ArrayList<E>(tree); 
     preorder.addAll((Collection<? extends Element>) array); 
     for (Element e : preorder) { 
      e.setLevel(0); 
     } 
     E1 = getAverageAccessTime(preorder); 
     listTwo = new ArrayList<Element>(preorder); 
     if ((E1 < E) || (rand.nextDouble() <= Math.exp(-(Math.abs(E1 - E))/0.5 * T))) { 
      E = E1; 
      listOne = listTwo; 

     } else { 
      noSolution++; 
     } 
     balance--; 
     System.out.println("running"); // EDITED 
     if (balance == 0 && noSolution ==1) {    
      noSolution = 0; 
      T = 0.95 * T;   

     } else if (balance == 0 && noSolution == 0) { 
      System.out.println("running");//EDITED 
      noSolution = 0; 
      return;//it doesn't work. 
     } 

    } else { 
     for (int i = 0; i < array.size(); i++) { 
      //create a list without the ith element 
      List<E> newList = new ArrayList<E>(array); 
      newList.remove(i); 
      //create a list by adding the ith element to beginning 
      List<E> newBeginning = new ArrayList<E>(tree); 
      newBeginning.add(array.get(i)); 
      rand_Function(newBeginning, newList); 
     } 
    } 

} 

是上述方法被調用的函數:

private void function(List<Element> list) { 
     noSolution = 0; 
     listOne = list; 
     T = 5; 
     balance = 3; 
     E = getAverageAccessTime(listOne); 
     for (Element e : listOne) { 
      e.setLevel(0); 
     } 
     rand_Function(emptyList, listOne); 
     System.out.println("coming out suddenly"); //I want to run this statement when I come out from the method above rand_Function(emptyList, listOne);suddenly! 



    } 

輸出將返回,我不希望它:

run: 
running 
running 
running 
return 
running 
running 
running 
running 
running 
coming out suddenly 

但我需要這個出來,我期待這一個:

run: 
running 
running 
running 
return 
coming out suddenly 
+0

我很困惑。如果這是完整的代碼示例,那麼函數將返回給調用方,無論您是否調用break break或什麼都不調用。你能詳細說明嗎? – Pablitorun 2011-03-04 18:39:39

+0

也我編輯了我的帖子對不起:) – user472221 2011-03-04 18:44:01

+0

鑑於顯示的代碼,返回語句無關緊要,該函數將立即返回。發佈的代碼或者不是真正的代碼,或者在問題描述中存在未傳達的內容。你能澄清你的意思是「突然出來」嗎?你能告訴我們會發生什麼,而不是你所期望的嗎? – Erik 2011-03-04 18:46:53

回答

6

你的代碼示例使用break;return; - 斷不會做的工作。

此外,由於您正在遞歸:return將不會返回多個呼叫級別。如果你想立即退出所有的遞歸調用,你需要發信號給調用者。

E.g.

public <E> boolean rand_Function(List<E> tree, List<E> array) { 

    if (array.isEmpty()) { 
     return true; 
    } 
    if (array.size() == 1) { 
     preorder = (List<Element>) new ArrayList<E>(tree); 
     preorder.addAll((Collection<? extends Element>) array); 
     for (Element e : preorder) { 
      e.setLevel(0); 
     } 
     E1 = getAverageAccessTime(preorder); 
     listTwo = new ArrayList<Element>(preorder); 
     if ((E1 < E) || (rand.nextDouble() <= Math.exp(-(Math.abs(E1 - E))/0.5 * T))) { 
      E = E1; 
      listOne = listTwo; 

     } else { 
      noSolution++; 
     } 
     balance--; 
     System.out.println("running"); // EDITED 
     if (balance == 0 && noSolution ==1) {    
      noSolution = 0; 
      T = 0.95 * T;   

     } else if (balance == 0 && noSolution == 0) { 
      System.out.println("running");//EDITED 
      return false; 
     } 

    } else { 
     for (int i = 0; i < array.size(); i++) { 
      //create a list without the ith element 
      List<E> newList = new ArrayList<E>(array); 
      newList.remove(i); 
      //create a list by adding the ith element to beginning 
      List<E> newBeginning = new ArrayList<E>(tree); 
      newBeginning.add(array.get(i)); 
      if (!rand_Function(newBeginning, newList)) 
       return false; 
     } 
    } 
    return true; 
} 
+0

+1有一個很好的地方。 – Endophage 2011-03-04 18:38:29

+0

當我寫「休息」,而不是「返回」時,它會顯示此錯誤:斷開開關或循環外! – user472221 2011-03-04 18:40:02

+0

'return'語句有效。你是否已經通過調試器瀏覽了這段代碼,看看會發生什麼? – Erik 2011-03-04 18:42:51

0

書面,你試圖用一個break時不是循環或交換機的內部。 return正常工作,因爲當列表爲空時,您在開始時使用它。

您還設置noSolution爲0,即使當它已經等於0

在問候你的編輯框將只執行,你命名你的變量時,需要約定。你有兩個變量是單個大寫字母。這會讓人們認爲它是一個泛型類型,尤其是當它們中的一個是T.另外,既然你只給了我們方法,我們必須假定所有未定義的變量都是類變量,但是可能有不明確的類型或狀態。

+0

是的,我檢查了它,但它並沒有突然出現! – user472221 2011-03-04 18:41:54