[ While 반복문 ]
package jun12;
//무한 반복문
//while, do~while 이 있습니다.
/*
while은 보통 무한 반복을 실행하다가 특정 조건이 되면 탈출하도록 합니다. 많이 사용됩니다.
채팅이나 게임 등 무한 반복이 필요한 로직에서 활용됩니다.
*/
public class While01 {
public static void main(String[] args) {
// 무한반복
while (true) {
System.out.println("참입니다.");
}
while(1 + 3 == 4) {
System.out.println("참입니다.");
}
boolean b = 1 + 3 == 2;
while(!b) {
System.out.println("참입니다.");
}
// 조건식이 참이라면 무한반복
// for문처럼 사용가능
int num = 1;
while (num < 10) {
System.out.println("참입니다.");
num++;
}
}
}
[ 입출력 + while + for문 ]
package jun12;
import java.io.IOException;
public class While02 {
public static void main(String[] args) throws IOException {
boolean quit = true;
while (quit) {
System.out.println("게임을 시작합니다.");
System.out.println("게임중...");
System.out.println("게임을 종료할까요?(Y/N)");
char input = (char) System.in.read(); // *
System.in.read(); System.in.read(); // *
if (input == 'Y' || input == 'y') {
System.out.println("게임을 종료합니다.");
quit = !quit;
}
System.in.skip(2); // *
}
}
}
* char input = System.in.read(); --> int타입!!!** (한글자(char)만 가능)
> char타입변환 + throws 해주기
* read는 엔터(= \n\r)도 따로 읽고 처리 > 출력문이 세 번 실행됨.
\n\r = (한줄내려+왼쪽정렬)
char input = (char) Syste m.in.read();
System..in.read(); Syste.in.read(); --> 엔터처리를 위해 두번 더 써줌.
\n에 대해처리 \r에 대해처리
* 다른값(한글자가 아닌 String) 입력하면 이후에 y 입력해도 종료되지 않음.
오류 없애기 위해 while문 마지막에 System.in.skip(2); 추가해줌.
게임을 시작합니다.
게임중...
게임을 종료할까요?(Y/N)
n
게임을 시작합니다.
게임중...
게임을 종료할까요?(Y/N)
y
게임을 종료합니다.
[ while문 실행가능/불가능한 경우 ]
package jun12;
public class While03 {
public static void main(String[] args) {
// 실행x
// while (!true) {
// System.out.println("출력문장"); *Error : 실행될 일이 없음
// }
//
// System.out.println("while문 밖 문장입니다.");
// 1)
boolean b = true;
while (b) {
System.out.println("출력문장");
}
System.out.println("while문 밖 문장입니다.");
}
// 2)
boolean b = true;
while (!b) {
System.out.println("출력문장");
}
System.out.println("while문 밖 문장입니다.");
}
}
출력문장
출력문장
출력문장
... // 1) 무한반복
while문 밖 문장입니다. // 2)
[ 입출력 + while + for문 ]
package jun12;
import java.util.Arrays;
import java.util.Scanner;
public class While04 {
public static void main(String[] args) {
// 1 ~ 100점
// 성적입력, 배열에 점수 저장
int[] score = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < score.length; i++) {
System.out.print(i + 1 + "번째 인원의 점수 입력 : ");
int input = sc.nextInt();
while (input < 0 || input > 100) {
System.out.print("다시 입력");
input = sc.nextInt();
}
score[i] = input;
}
// 출력
System.out.println(Arrays.toString(score));
sc.close();
}
}
1번째 인원의 점수 입력 : 80
2번째 인원의 점수 입력 : 90
3번째 인원의 점수 입력 : 120
다시 입력70
4번째 인원의 점수 입력 : 98
5번째 인원의 점수 입력 : 100
[80, 90, 70, 98, 100]
package jun12;
public class While05 {
public static void main(String[] args) {
// 조건식 검사 후 내부 명령어를 실행합니다.
int num = 0;
while(num < 3) {
System.out.println(num++ + "번 반복중입니다.");
}
// or
int num = 0;
while(num < 3) {
System.out.println(num + "번 반복중입니다.");
num++; // ++num도 가능
}
}
}
0번 반복중입니다.
1번 반복중입니다.
2번 반복중입니다.
'국비과정 > JAVA (기초)' 카테고리의 다른 글
20230612 _[7일차]_04. 배열 정렬 (0) | 2023.06.13 |
---|---|
20230612 _[7일차]_03. Do ~ While문 (0) | 2023.06.13 |
20230612 _[7일차]_02. break, continue (0) | 2023.06.12 |
20230612 _[7일차]_01. 알고리즘 & break, continue (0) | 2023.06.12 |
20230609 _[6일차]_06. 2차원배열 + 랜덤숫자 요소채우기 (0) | 2023.06.10 |