Jak wyjść z dwóch zagnieżdżonych pętli [duplicate]

To pytanie ma już odpowiedź tutaj:

Używam Javy od dłuższego czasu, ale brakuje mi edukacji w pętlach. Wiem, jak stworzyć każdą pętlę, która istnieje w Javie i z niej wyłamać. Jednak ostatnio myślałem o tym:

Powiedzmy, że mam dwie zagnieżdżone pętle. Czy Mogę wyłamać się z obu pętli używając tylko jednej break instrukcji?

Oto, co mam do tej pory.
int points = 0;
int goal = 100;
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break; //for loop ends, while loop does not
      }
   //I know I could put a 'break' statement here and end the while loop but I want to do it using just one 'break' statement
   points += i;
   }
}
Czy jest jakiś sposób, aby to osiągnąć?
Author: fireshadow52, 2011-07-10

6 answers

W języku java możesz użyć etykiety, aby określić, która pętla ma być przerwana/kontynuowana:

mainLoop:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break mainLoop;
      }
      points += i;
   }
}
 82
Author: sirbrialliance,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-10 00:09:46

Tak, możesz napisać z etykietą np.:

int points = 0;
int goal = 100;
someLabel:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break someLabel;
      }
   points += i;
   }
}
// you are going here after break someLabel;
 19
Author: Grzegorz Szpetkowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-10 00:10:04

Jest wiele sposobów na oskórowanie tego kota. Oto jeden:

int points = 0;
int goal = 100;
boolean finished = false;
while (goal <= 100 && !finished) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         finished = true;
         break;
      }
   points += i;
   }
}

Update : Wow, nie wiedziałem o zerwaniu z etykietami. To chyba lepsze rozwiązanie.

 6
Author: Dan Tao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-10 00:10:20

Elementarne, drogi Watsonie ...

int points = 0;
int goal = 100;

while (goal <= 100) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal++;
      break;
    }
  points += i;
  }
}

Lub

int points = 0;
int goalim = goal = 100;

while (goal <= goalim) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal = goalim + 1;
      break;
    }
  points += i;
  }
}
 1
Author: Blessed Geek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-10 00:17:58

Możesz zresetować zmienne sterowania pętlą.

int points = 0;
int goal = 100;
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         i = goal = 101;
      }
   points += i;
   }
}
 0
Author: Dipto_Das,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-18 17:17:09

Nie należy używać etykiet w języku obiektywnym. Musisz przepisać warunek dla / while.

Więc Twój kod powinien wyglądać następująco:

int points = 0;
int goal = 100;
while (goal <= 100 && points <= 50) {
   for (int i = 0; i < goal && points <= 50; i++) {
       points += i;
   }
}

// now points are 55 
 0
Author: Kacper Obrzut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-25 09:31:35