我有一個簡單的代碼是這樣的:停留在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);
}
}
這聽起來像是某種東西在多次調用此代碼... –
這是否甚至編譯?看起來你在'else'之前丟失了一個右大括號。 –
你所提供的代碼不會編譯 - 你有一個'else'在它之前沒有大括號,並且在它之前沒有單個語句'if'。請提供一個簡短的*完整*程序來證明問題。 –