2017-08-10 345 views
-1

我讀通過這個格式的文本文件:可以在for循環中使用多個continue語句嗎?

Quec 
    23 
    Pirata 
    121 
    Culiacan 
    85 
    Asi 
    55 
    Nomas 
    0 
    Quedo 
    0 
    Viejon 
    0 
STARTS AGAIN 
    Quec 
    48 
    Pirata 
    54 
    Culiacan 
    5 
    Asi 
    76 
    Nomas 
    2 
    Quedo 
    69 
    Viejon 
    8 

正如你所看到的名字重複兩次,但一旦名單再次開始他們被分配不同的值。我必須在列表中應用一些邏輯。我目前有這個for循環

for(int i =0;i<100;i++) 
{ 
    //nameArray stores the names found in text file 
    if(nameArray[i].equals("Quec")) //Repeat this for every name 
      //DO LOGIC 
} 

我基本上需要做的邏輯與第一組數據和第二組數據重複相同的邏輯。是否可以使用多個繼續語句來實現這一點?還是有另一種方法來做到這一點?

+1

是否可以在for循環中使用多個continue語句?是。還有另一種方法可以做到嗎?是。 https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Javier

回答

0

你能用這樣的東西來解決問題嗎?

private void processFile(Scanner in, int groups) { 
    for (int groupId=0; groupId<groups; group++) { 
     processGroup(in, groupId); 
    } 
} 

private void processGroup(Scanner in, int groupId) { 
    for (int groupRow=0; groupRow<FIELDS_PER_GROUP; groupRow++) { 
     String name=in.next(); 
     int value=in.nextInt(); 
     processEntry(name, value, group); 
    } 
} 

private void processEntry(String name, int value, int group) { 
    // do some logic 
}