먼저 break문을 실습해보자 !
package interationex;
public class BreakEx {
public static void main(String[] args) {
// 중첩 반복문은 중지할 반복문의 이름을 주고 break
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
// j의 값이 3이상이 되면 break문으로 인해 2번째 for문을 빠져나갈 수 있음
if (j==3) {
break;
}
System.out.println("i="+i+", j="+j);
}
}
}
}

package interationex;
public class BreakEx {
public static void main(String[] args) {
// 중첩 반복문은 중지할 반복문의 이름을 주고 break
for (int i=0; i<5; i++) {
if (i%2==0) {
continue;
}
System.out.println(i);
}
}
}

package interationex;
import java.util.Scanner;
public class OndRandomGame {
public static void main(String[] args) {
int com = (int)(Math.random()*100);
Scanner sc = new Scanner(System.in);
int cnt = 0;
while(true) {
cnt++;
System.out.println("user >>");
int user = sc.nextInt();
if(com == user) {
break;
}
if(user > com) {
System.out.println("더 작아야해 !!");
} else if (user < com) {
System.out.println("더 커야해 !!");
}
}
System.out.printf("%s회만에 성공해버렸어 !!",cnt);
}
}
