do ~ while 문
- 스코프 { }를 최소한 한 번 이상 반복
- 사용자 입력을 받는 경우 주로 사용
do {
// 처음 한 번은 무조건 실행
// 조건식이 만족할 시 반복 수행
} while (조건식);
1 ~ 임의의 수(1~10)까지 의 합계
public void method2() {
int nRandom = ((int)(Math.random()*10)+1);
int nSum = 0;
int i = 0;
do {
i++;
nSum += i;
System.out.println("nRandom: " + nRandom);
System.out.printf("i: %d, nSum: %d\n", i, nSum);
} while(i < nRandom);
System.out.println("Total: " + nSum);
}
출력물
nRandom: 5
i: 1, nSum: 1
nRandom: 5
i: 2, nSum: 3
nRandom: 5
i: 3, nSum: 6
nRandom: 5
i: 4, nSum: 10
nRandom: 5
i: 5, nSum: 15
Total: 15
다른 클래스에 있는 프로그램 실행하는 프로그램
public void method3() {
char cMenu = ' ';
Scanner s = new Scanner(System.in);
C_For example = new C_For();
do {
System.out.println();
System.out.println("============================");
System.out.println("1. Excute new C_For.method1()");
System.out.println("2. Excute new C_For.method2()");
System.out.println("3. Excute new C_For.method3()");
System.out.println("Q(q).Program Quie");
System.out.println("============================");
System.out.println();
System.out.printf("Insert Number> ");
cMenu = s.nextLine().charAt(0);
switch (cMenu) {
case '1' :
System.out.println("[Menu 1]");
example.method1();
break;
case '2' :
System.out.println("[Menu 2]");
example.method2();
break;
case '3' :
System.out.println("[Menu 3]");
example.method3();
break;
case 'q' :
case 'Q' :
System.out.println("QUIT!!\n");
return;
default :
System.out.println("\n... Try again\n");
}
} while(true);
}
출력문
============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 1
[Menu 1]
Result: 55
============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 2
[Menu 2]
[ 2 단] [ 3 단] [ 4 단] [ 5 단] [ 6 단] [ 7 단] [ 8 단] [ 9 단]
2 * 1 = 2 3 * 1 = 3 4 * 1 = 4 5 * 1 = 5 6 * 1 = 6 7 * 1 = 7 8 * 1 = 8 9 * 1 = 9
2 * 2 = 4 3 * 2 = 6 4 * 2 = 8 5 * 2 = 10 6 * 2 = 12 7 * 2 = 14 8 * 2 = 16 9 * 2 = 18
2 * 3 = 6 3 * 3 = 9 4 * 3 = 12 5 * 3 = 15 6 * 3 = 18 7 * 3 = 21 8 * 3 = 24 9 * 3 = 27
2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 5 * 4 = 20 6 * 4 = 24 7 * 4 = 28 8 * 4 = 32 9 * 4 = 36
2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 6 * 5 = 30 7 * 5 = 35 8 * 5 = 40 9 * 5 = 45
2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 7 * 6 = 42 8 * 6 = 48 9 * 6 = 54
2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 8 * 7 = 56 9 * 7 = 63
2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 9 * 8 = 72
2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> 3
[Menu 3]
===============
q or Q = Quit
===============
Please Insert key(q or Q): q
==== Bye Bye Bye ====
============================
1. Excute new C_For.method1()
2. Excute new C_For.method2()
3. Excute new C_For.method3()
Q(q).Program Quie
============================
Insert Number> q
QUIT!!