while 문
- 조건을 만족시키는 동안 스코프 { }를 반복 수행
- 반복 횟수를 모를 때 주로 사용
while (조건식) {
// 조건에 만족할 시 반복 수행
}
exit 입력 시 프로그램 종료
public void method2() {
String sInput = "";
Scanner s = new Scanner(System.in);
while(sInput.equalsIgnoreCase("exit")) {
System.out.printf("Insert String: ");
sInput = s.nextLine();
System.out.println(sInput);
}
System.out.println("Bye~!");
}
출력물
Insert String: asdf
asdf
Insert String: EXIT
EXIT
Bye~!
사용자가 입력한 구구단 출력
public void method3() {
boolean bFlag = true;
int nDan = 2;
Scanner s = new Scanner(System.in);
while(bFlag) {
int nNum = 1;
System.out.printf("Insert DAN(2~9): ");
nDan = s.nextInt();
if(2 >= nDan || nDan <= 9) {
System.out.printf("=== %d 단 ===\n", nDan);
while(nNum <= 9) {
System.out.printf("%d * %d = %2d\n", nDan, nNum, (nDan * nNum));
nNum++;
}
} else {
System.out.println("Are your eyes twisted?\n");
}
bFlag = false;
}
}
출력물
Insert DAN(2~9): 0
Are your eyes twisted?
Insert DAN(2~9): 8
=== 8 단 ===
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
'Developer TABLE > Java' 카테고리의 다른 글
반복문 do ~ while (0) | 2021.08.26 |
---|---|
중첩 for 문 (0) | 2021.08.26 |
반복문 for (0) | 2021.08.26 |