[英]how to avoid code from execution which are outside of the loop
我有一種方法,其中我比較for循環內的arraylist值。 如果loop1有任何單個條件為真,我就不想執行Loop 2
(請參見下面的代碼)。
現在的問題是,對於某些值, loop 1
可以統計,而對於某些其他值, loop 2
可以滿足。由於這個原因,我在db中填充了錯誤的數據。
我想以這種方式修改我的代碼。 如果任何數組列表值滿足loop 1
則編譯器應從return ERROR
。 在那之后它不應該執行代碼。
循環檢查條件為if(quant>Integer.parseInt(book.getQuantity()))
Action.java。
public String execute()
{
if(id.length == quantity.length)
{
for (int i = 0; i < id.length; ++i)
{
book = dao.listbookdetailsByBId(id[i]);
Double dq=new Double(quantity[i]);
int quant=dq.intValue();
if(quant>Integer.parseInt(book.getQuantity()))
{
//Loop 1 , this is executing if any of the quant is greater then book.getQuantity()..
//i want to stop executing LOOP2 ,if any of the value of quant is greater then book.getQuantity().
addActionError("You have entered an invalid quantity for the Book Title- ''"+book.getBookTitile()+"''.");
return ERROR;
}
/* Loop2 starts here
* Loop 2 , this is executing if any of the quant is lesser ..
* The below code should execute only if compiler does not reach to the loop1 */
// Some DAO code goes here
}
}
return SUCCESS;
}
只需使用break語句
if(quant>Integer.parseInt(book.getQuantity()))
{
//Loop 1 , this is executing if any of the quant is greater then book.getQuantity()..
//i want to stop executing LOOP2 ,if any of the value of quant is greater then book.getQuantity().
addActionError("You have entered an invalid quantity for the Book Title- ''"+book.getBookTitile()+"''.");
break;
}
您應該使用break語句來實現此目的,或者可以創建一個局部布爾變量,並使用它避免進入循環2。
if(quant>Integer.parseInt(book.getQuantity()))
{ flag=false;
}
if(flag){
//loop 2
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.