2011-07-25 76 views
-6

我有一個簡單的代碼是這樣的:停留在while循環

try { 
    int i = 0; 
    while (i < size) { 
     System.out.println("i is" + i); 
     if (someCondition) { 
      System.out.println("do sth"); 
      someCondition = true; 
      i++; 
      System.out.println("i is" + i); 
     } else { 
      System.out.println("doAnotherThing"); 
      someCondition = true; 
      i++; 
      System.out.println("i is" + i); 
     } 
    } 
} catch(Exception){ 

} 

此代碼段的輸出是:

i is 0 
do sth 
i is 1 
i is 0 
doAnotherThing 
i is 1 

它應該有增加我,比同時迴路斷線,但它不」噸。你對這個問題有什麼看法嗎?我從5個小時開始就處理這個問題,也許我錯過了一些東西。如果你能幫助我,我會很高興。
在此先感謝。

編輯:我想事情簡單化,但appearantly它沒有工作:) OK這裏是真正的代碼:

public void (Analyzer analyzer){ 
    try {  
     int i=0; 
     while (i < analyzer.size()) { 
      System.out.println("i is" + i); 
      Object anInstance = analyzer.getObject().get(i); 
      if (anInstance.getDatabaseCreated()) { //this comes from another class and is //false in the first place       
       dropObject(analyzer.getId(),i); // removes object 
       createAnInstance(analyzer.getId(), i, anInstance.getTypes()); //creates another instance       
       anInstance.markCreated(); 

       Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());       

       for (int j = 0; j < anInstance.rowCount(); j++) { 
        insertRow(query, anInstance.getTypes(), anInstance.getRow(j)); 
       } 
       i++; 
       System.out.println("i is" + i); 
      } else {        
       createAnInstance(analyzer.getId(), i, anInstance.getTypes()); 
       anInstance.markCreated(); 
       Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes()); 
       for (int j = 0; j < anInstance.rowCount(); j++) { 
        insertRow(query, anInstance.getTypes(), anInstance.getRow(j));         
       } 
       i++; 
       System.out.println("i is" + i); 
      } 
     } 
    } catch (Exception ex) { 
     Logger.getLogger(AnalyzerService.class.getName()).log(Level.SEVERE, null, ex); 
     throw new RuntimeException(ex); 
    } 
} 
+1

這聽起來像是某種東西在多次調用此代碼... –

+0

這是否甚至編譯?看起來你在'else'之前丟失了一個右大括號。 –

+2

你所提供的代碼不會編譯 - 你有一個'else'在它之前沒有大括號,並且在它之前沒有單個語句'if'。請提供一個簡短的*完整*程序來證明問題。 –

回答

0

我認爲,爲了得到你輸出的結果,必須有兩個不同的調用方法(在你的代碼片段中沒有名字)。看來這個方法被調用,我被設置爲0,循環開始,第一個條件成立。然後循環結束(因爲analyzer.size()是1,或者可能是一個異常???),然後在代碼中的其他地方再次調用此方法。再次,我設置爲0,但這次第一個條件是錯誤的(第一次調用方法???的結果),所以你通過else來代替。循環在迭代1次後結束,因爲analyzer.size()是1或引發異常。

+0

你說得對,這種方法被稱爲兩次。由於我沒有自己寫代碼,所以我沒有意識到這個錯誤。謝謝你的回答。 – lamostreta

0

你爲什麼要設置someCondition爲true兩個ifelse?此外,請檢查someCondition是否將i的值重置爲0或其他值。不知道someCondition是什麼,很難猜測。但如果它是一個以i的值作爲參數的函數,我會檢查它。

+0

參數按Java值傳遞... –

+0

需要重置循環內的someCondition。但問題不在那裏。問題是這個方法被其他類調用過兩次。感謝您的回答。 – lamostreta

0

此代碼的假 - 包圍甚至沒有排隊。正如所寫,這將工作假設你初始化大小變量,否則大小可能是巨大的(隨機整數像384923492348),它會做很多迭代。如果你想有一個更好的答案(只是複製粘貼?:))

+0

我不想用一些無意義的代碼來打擾人們,這就是爲什麼我重寫SO的代碼。令人羨慕的人在這裏喜歡真正的代碼。無論如何,感謝您的回答:) – lamostreta

0

在我看來,那是dropObject改變analyzer.size()而後者的大小是擺在首位1您可以修復代碼了。

+0

這不會解釋我的輸出是1,其次我是0,因爲我唯一的操作是增加它。 – DaveJohnston

0

很明顯,analyzer.size()是變異。由於我不知道其他來源,所以我無法確切知道是什麼原因造成的。下面是可能的罪犯名單。)

  • analyzer.size
  • analyzer.getObject(獲得
  • anInstance.getDatabaseCreated
  • dropObject
  • markCreated
  • analyzer.getId
  • 分析儀.getTypes
  • anInstance.getRow
  • anInstance。getTypes
  • 的insertRow

如果您不需要擔心會發生什麼,當你添加行查詢,你可以提前只需緩存大小分析儀,但你可能會遇到麻煩,因爲它看起來你正在刪除一些值,同時添加其他值。如果這沒有關係,但是,它是那樣簡單:

int i=0; 
int sz = analyzer.size() 
while (i < sz) { 

另外,我注意到,有大量的複製和粘貼代碼。糟糕的交易。你的整個while循環可能是:

// you left out the function name! 
public void theNamelessOne(Analyzer analyzer){ 
    try {  
     int i=0; 
     while (i < analyzer.size()) { 
      System.out.println("i is" + i); 
      // the only time you should really use an article ("a", "an", "the" 
      // or equivalent) in a method or vaeriable name is when referencing 
      // "the doctor" or "the wabbit". since there is only one "the doctor" 
      // and this is not an Elmer Fudd related instance, you may want to 
      // remove it. 
      Object anInstance = analyzer.getObject().get(i); 
      if (anInstance.getDatabaseCreated()) { 
       dropObject(analyzer.getId(),i); 
      } 
      createAnInstance(analyzer.getId(), i, anInstance.getTypes()); 
      anInstance.markCreated(); 
      Query query = createInsertQuery( analyzer.getId(), 
               i, 
               anInstance.getTypes()); 
      for (int j = 0; j < anInstance.rowCount(); j++) { 
       insertRow(query, anInstance.getTypes(), anInstance.getRow(j)); 
      } 
      i++; 
      System.out.println("i is" + i); 
     } 
    } catch (Exception ex) { 
     Logger.getLogger(
      AnalyzerService.class.getName() 
     ).log(Level.SEVERE, null, ex); 
     throw new RuntimeException(ex); 
    } 
} 
+0

謝謝你的回答。您的代碼比我的代碼更清晰,我將保留它作爲未來使用的參考。但是我提供的代碼非常有效。我找到了錯誤的原因:這個函數被調用兩次。由於我沒有編寫代碼,所以我沒有意識到這個錯誤。無論如何,非常感謝您的長期解釋。 – lamostreta