2016-11-19 34 views
0

我只是在這裏停留了五個小時,我只想獲得信息的價值,這是櫃檯,並從零開始,但是當給定的條件滿足,我只是想得到櫃檯的價值。有沒有更好,更簡單的方法來獲得這個?
我下面的代碼:java如何停止循環,並獲得計數器的值

for(int info = 0; info<currentData.length();info++) { 
    JSONObject managedata = currentData.getJSONObject(currentData.length() - 1); 
    long getcurrenttime = managedata.getLong("dt"); 

    while (getcurrenttime > currentEpochTime) { 
     // i want to get the 'info' value here when the following condition meet. 
    } 

回答

0

試試這個

for(int info = 0; info<currentData.length();info++) { 
    JSONObject managedata = currentData.getJSONObject(currentData.length() - 1); 
    long getcurrenttime = managedata.getLong("dt"); 

    if (getcurrenttime > currentEpochTime) { 
     // i want to get the 'info' value here when the following condition meet. 

     System.out.println(info); 
     // make info=0 if you want to start the loop again from begining 
     // else use break to break the for loop when condition is meet 

     // Use this 
     // info = 0 

     // or this depending on your usecase 
     //break 
    } 
+0

感謝,但現在幫助,我只是想獲取信息的價值它將停止,它將打印從零開始的所有信息值。 – suresh

0

需要之前創建一個變量循環的信息價值從循環存儲如下:

//some method 
int storedInfo=0; 
for(int info = 0; info<currentData.length();info++) { 
    JSONObject managedata = currentData.getJSONObject(currentData.length() - 1); 
    long getcurrenttime = managedata.getLong("dt"); 

    if (getcurrenttime > currentEpochTime) { 
     // i want to get the 'info' value here when the following condition meet. 
     storedInfo=info; 
     //you can break further looping as condition met and you have stored info value above 
     break; 

    } 
0

你當病情發展時應該休息。我還介紹了一個布爾值,所以我知道如果條件匹配至少一次:

int info = 0; 
boolean conditionMatch = false; 

for(int info = 0; info<currentData.length();info++) { 
    JSONObject managedata = currentData.getJSONObject(currentData.length() - 1); 
    long getcurrenttime = managedata.getLong("dt"); 

    while (getcurrenttime > currentEpochTime) { 
     // i want to get the 'info' value here when the following condition meet. 
     conditionMatch = true; 
     break; 
    } 
} 
if(conditionMatch){ 
    System.out.println("Condition matched when counter was:" + info); 
else{ 
    System.out.println("Condition not matched"); 
} 
0

嘗試下面的代碼 -

int info = 0; 
    for(; info<currentData.length();info++) { 
     JSONObject managedata = currentData.getJSONObject(currentData.length() - 1); 
     long getcurrenttime = managedata.getLong("dt"); 

     if (getcurrenttime > currentEpochTime) { 
      // i want to get the 'info' value here when the following condition meet. 
      break; 

     } 
    } 
    if(info != currentData.length()){ 
     System.out.println(info); 
    }