자바에서 for 루프에서 벗어나기
내 코드에는 for 조건을 충족 할 때까지 코드 메서드를 반복하는 for 루프가 있습니다.
어쨌든이 for 루프에서 벗어날 수 있습니까?
따라서 아래 코드를 살펴보면 "15"에 도달했을 때이 for 루프를 빠져 나가고 싶다면 어떻게해야할까요?
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Outputs:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
나는 소용이없는 다음을 시도했습니다.
public class Test {
public static void main(String args[]) {
boolean breakLoop = false;
while (!breakLoop) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
if (x = 15) {
breakLoop = true;
}
}
}
}
}
그리고 루프를 시도했습니다.
public class Test {
public static void main(String args[]) {
breakLoop:
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
if (x = 15) {
break breakLoop;
}
}
}
}
내가 원하는 것을 얻을 수있는 유일한 방법은 for 루프를 벗어나는 것입니다. 잠시 동안 그것을 대체 할 수 없습니다. do, if etc 문.
편집하다:
이것은 예제로만 제공되었으며 구현하려는 코드가 아닙니다. 이제 각 루프가 시작되는 위치 뒤에 여러 IF 문을 배치하여 문제를 해결했습니다. 중단이 없어 루프의 한 부분에서 onlu가 튀어 나오기 전에;
break;
is what you need to break out of any looping statement like for
, while
or do-while
.
In your case, its going to be like this:-
for(int x = 10; x < 20; x++) {
// The below condition can be present before or after your sysouts, depending on your needs.
if(x == 15){
break; // A unlabeled break is enough. You don't need a labeled break here.
}
System.out.print("value of x : " + x );
System.out.print("\n");
}
You can use:
for (int x = 0; x < 10; x++) {
if (x == 5) { // If x is 5, then break it.
break;
}
}
If for some reason you don't want to use the break instruction (if you think it will disrupt your reading flow next time you will read your programm, for example), you can try the following :
boolean test = true;
for (int i = 0; i < 1220 && test; i++) {
System.out.println(i);
if (i == 20) {
test = false;
}
}
The second arg of a for loop is a boolean test. If the result of the test is true, the loop will stop. You can use more than just an simple math test if you like. Otherwise, a simple break will also do the trick, as others said :
for (int i = 0; i < 1220 ; i++) {
System.out.println(i);
if (i == 20) {
break;
}
}
How about
for (int k = 0; k < 10; k = k + 2) {
if (k == 2) {
break;
}
System.out.println(k);
}
The other way is a labelled loop
myloop: for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break myloop;
}
System.out.println(i + " " + j);
}
}
For an even better explanation you can check here
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
if(x==15)
break;
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
참고URL : https://stackoverflow.com/questions/15275195/breaking-out-of-a-for-loop-in-java
'Development Tip' 카테고리의 다른 글
UIToolBar 왼쪽 및 오른쪽 패딩 조정 방법 (0) | 2020.12.02 |
---|---|
EF4-선택한 저장 프로 시저가 열을 반환하지 않음 (0) | 2020.12.02 |
생년월일을 기준으로 연령 계산 (0) | 2020.12.02 |
Swift의 UITableView (0) | 2020.12.02 |
iPhone X / 8 / 8 Plus CSS 미디어 쿼리 (0) | 2020.12.02 |