(1) do ~ while문 : 선실행, 후조건, 최소 한번은 실행해야 하는 code

형식)

do {

실행문;

} while(조건식);

 

* while문과 비교

  • 0열 선택0열 다음에 열 추가
  • 1열 선택1열 다음에 열 추가
  • 0행 선택0행 다음에 행 추가
  • 1행 선택1행 다음에 행 추가
  • 2행 선택2행 다음에 행 추가
  • 3행 선택3행 다음에 행 추가

셀 전체 선택

열 너비 조절

행 높이 조절

while

do ~ while

선조건, 후실행

선실행, 후조건

 

반복횟수가 0이어도, 조건에 상관없이 무조건 한번은 실행됨

사용자가 입력한 수의 팩토리얼을 구하는 프로그램

메뉴를 나타내어 사용자의 입력을 처리하는 프로그램

(사용자의 입력을 받은 후에 계속 루프를 돌 것인지 판단할 때 사용)

public class DoWhileTest{
	public static void main(String[] args) {
		int x = 0;
		do{
			x = (int )(Math.random() *3);
			System.out.println("x :" + x);
		}
		while (x!=0); //0이 되면 종료
	}
}

- 로또 do~while문으로 풀기

/*while문은 앞에 난수발생을 반복해서 써줘야 하지만, 
do ~ while문은 선실행, 후조건이기 때문에 앞에서 초기화값을 지정하지 않아도 ok~*/

public class Lotto2{
	public static void main(String[] args) {
		int gameCnt = 5;
		System.out.println("===== 금주의 예상번호 =====");
		int n1,n2,n3,n4,n5,n6;
		for (int i = 1;i <=gameCnt ;i++ ){
			n1 = (int) (Math.random() * 45 + 1);
			do{
				n2 = (int) (Math.random() * 45 + 1);
			}
			while (n1 == n2);
		
			do{
				n3 = (int) (Math.random() * 45 + 1);
			}
			while (n1 == n3 || n2 == n3);
		
			do{
				n4 = (int) (Math.random() * 45 + 1);
			}
			while (n1 == n4 || n2 == n4 || n3 == n4);
		
			do{
				n5 = (int) (Math.random() * 45 + 1);
			}
			while (n1 == n5 || n2 == n5 || n3 == n5 || n4 == n5);
			
			do{
				n6 = (int) (Math.random() * 45 + 1);
			}
			while (n1 == n6 || n2 == n6 || n3 == n6 || n4 == n6 || n5 == n6);
			
			System.out.println(i + "게임 : " + n1 + "  " + n2 + "  " + n3 + "  " + n4 + "  " + n5 + "  " + n6);
			}	 
		}
	}

- 숫자 야구게임 (1번과 2번까지만 배움... 추후에 완성시키는 걸로~~)

/*
1. 컴이 서로 다른 3자리수를 발생 
	- 첫자리는 0이 되면 안된다.
	- 나머지는 0~9중의 난수.
2. 내가 입력한 수를 3개로 분리 (유효성검사 - 입력값이 적절한지)
3. 컴과 내가 입력한 수를 비교
 3-1. 숫자와 자리까지 맞으면 strike
 3-2. 숫자만 맞고 자리가 틀렸으면 ball
4. 결과 출력
 4-1. strike가 3이면.. xxx는 정답입니다.
 4-2. strike가 3이 아니면... xxx는 x스트라이크 X볼입니다.
*/

public class BaseBallGame{
	public static void main(String[] args) {
		int myNum = 729;
		int c1, c2, c3;
		int m1 = (myNum /100);
		int m2 = (myNum % 100 /10);
		int m3 = (myNum % 10);
		
		do{
			c1 = (int) (Math.random()*10);
		}
		while (c1 == 0);

		do{
			c2 = (int) (Math.random()*10);
		}
		while (c1 == c2);

		do{
			c3 = (int) (Math.random()*10);
		}
		while (c1 == c3 || c2 == c3);

		System.out.println("컴 : " +c1 + " " +c2+ " " +c3);//컴 : x x x
		System.out.println("나 : "+m1 + " " +m2+ " " +m3);//컴 : 7 2 9
		
	}
}

 

(2) break문 : 반복문인 for, while, do~while문을 실행 중지시킬 때 사용 (if문과 사용)

: if문의 조건문이 true라면, 아예 반복문을 빠져나오도록 설정!!

 

public class BreakExample{
	public static void main(String[] args) {
		while(true) {
			int num = (int) (Math.random()*6)+1;
			System.out.println(num);
			
			if (num == 6){ //6이 나오면 멈추고 while문을 빠져나가 "프로그램 종료"를 출력하라.
				break;
			}
		}
		System.out.println("프로그램 종료");
	}
}

 

(3) continue문 : 반복문인 for, while, do~while문에서만 사용 (if문과 사용)

if문의 조건문이 true라면 continue 아래는 실행되지 않고, 위로 반복문을 찾아 올라간다.

- for문 : 증감식으로 이동

- while, do~while문 : 조건식으로 이동

 

//무한루프

public class EtcTest{
	public static void main(String[] args) {
		boolean b = true;
		while (b) {
			int x = (int) (Math.random() *5); // 0~4
			if (x == 0){
				b = false;
			} else {
				System.out.println("10 / x :" + 10 / x);
			}
		}
		System.out.println("---------------------");
		while (true){
			int x = (int) (Math.random() *5);
			if (x == 0){
				System.out.println("0이 나와서 종료합니다.");
				break; //반복문을 빠져나오는 break, 주로 if문이랑 같이 쓰임
			}
			System.out.println("10 / x :" + 10 / x);

		}

		System.out.println("---------------------");
		for (int i = 0;i<10 ;i++ ){
			int x = (int) (Math.random() *5);
			if (x == 0){
				System.out.println("0이 나와서 종료합니다.");
				break;
			}
			System.out.println(i + 1 + ". 10 / x :" + 10 / x);
		}

		System.out.println("---------------------");
		for (int i = 0;i<10 ;i++ ){
			int x = (int) (Math.random() *5);
			if (x == 0){
				System.out.println("0이 나와서 다음으로 진행합니다.");
				continue;
			}
			System.out.println(i + 1 + ". 10 / x :" + 10 / x);
		}
	}
}
---------- run ----------
---------------------
10 / x :10
10 / x :10
10 / x :2
10 / x :3
10 / x :3
0이 나와서 종료합니다.
---------------------
0이 나와서 종료합니다.
---------------------
1. 10 / x :3
0이 나와서 다음으로 진행합니다.
3. 10 / x :3
4. 10 / x :3
5. 10 / x :3
6. 10 / x :5
7. 10 / x :5
8. 10 / x :3
0이 나와서 다음으로 진행합니다.
10. 10 / x :5

+ Recent posts