JAVA/code by me

Operation Program - java.io package의 BufferedReader + while(true) break; continue;

Y_____527 2021. 1. 27. 23:40

▶BufferedReader의 readLine()을 이용한 연산 프로그램 

docs.oracle.com/javase/8/docs/api/

 

Java Platform SE 8

 

docs.oracle.com


<잘못된 코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package quiz1.ex01.io;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
//숫자 2개, 연산자 1개 입력받아서 사칙연산 프로그램 작성(io - BufferedReader)
public class MainEntry {
 
    public static void main(String[] args) throws IOException {
 
        String op = null;
        int su1 = 0, su2 = 0;
        double result = 0.;
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
 
            while (true)
                second: {
                    System.out.println("숫자1을 입력하세요");
                    su1 = Integer.parseInt(br.readLine());
 
                    System.out.println("숫자2를 입력하세요.");
                    su2 = Integer.parseInt(br.readLine());
 
                    System.out.println("숫자1과 숫자2의 연산에 사용할 연산자를 고르세요.(+, -, *, /)");
                    op = br.readLine();
                    
                    switch (op) {
                    case "+":
                        result = su1 + su2;
                        break second;
                    case "-":
                        if (su1 >= su2) {
                            result = su1 - su2;
                        } else {
                            result = su2 - su1;
                        }
                        break second;
                    case "*":
                        result = su1 * su2;
                        break second;
                    case "/":
                        if (su1 >= su2) {
                            result = su1 / su2;
                        } else {
                            result = su2 / su1;
                        }
                        break second;
                    default
                        System.out.println("+,-,*,/ 중에 입력해주세요.");
                        continue;
                    
                    } // switch end
 
                } // while end
        } catch (Exception e) {
            e.printStackTrace();
        }
//==================!무한루프에서 빠져나오지 못함!==================ㅜㅜ
        if (su1 >= su2) {
            System.out.printf("%d %s %d = %.2f", su1, op, su2, result);
 
        } else {
            System.out.printf("%d %s %d = %.2f", su2, op, su1, result);
        }
    }// main end
}
 
cs

-while(true)의 무한루프를 빠져나오지 못한다.

-while(true) second: { -> 식별자를 넣어

  break second;를 해도 while을 빠져나가는게 아니라, 다시 while문을 시작한다.

-default: 

     continue; 와 break; 와 아무것도 작성하지 않아도 모두 while문으로 돌아가 시작된다.


<완성한 코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package quiz1.ex01.io;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
//숫자 2개, 연산자 1개 입력받아서 사칙연산 프로그램 작성(io - BufferedReader)
public class MainEntry2 {
    public static void main(String[] args) {
 
        String op = null;
        int su1 = 0, su2 = 0;
        double result = 0.;
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        System.out.println("<<<<<operate program>>>>>");
        try {
            while (true) {
                System.out.println("숫자1을 입력하세요");
                su1 = Integer.parseInt(br.readLine());
 
                System.out.println("숫자2를 입력하세요.");
                su2 = Integer.parseInt(br.readLine());
 
                System.out.println("숫자1과 숫자2의 연산에 사용할 연산자를 고르세요.(+, -, *, /)");
                op = br.readLine();
 
                switch (op) {
                case "+":
                    result = su1 + su2;
                    break;
                case "-":
                    if (su1 >= su2) {
                        result = su1 - su2;
                    } else {
                        result = su2 - su1;
                    }
                    break;
                case "*":
                    result = su1 * su2;
                    break;
                case "/":
                    if (su1 >= su2) {
                        result = su1 / su2;
                    } else {
                        result = su2 / su1;
                    }
                    break;
                default:
                    System.out.println("연산자를 다시 입력해주세요");
                    continue;
                }// switch end
 
                if (su1 >= su2) {
                    System.out.printf("%d %s %d = %.2f", su1, op, su2, result);
 
                } else {
                    System.out.printf("%d %s %d = %.2f", su2, op, su1, result);
                }
 
                System.out.println("\n다시 입력하시겠습니까?");
                String re = br.readLine();
                if (re.equalsIgnoreCase("y")) {
                   continue; //==>안써도 다시 반복됨!!
                } else {
                    System.out.println("계산프로그램을 종료합니다.");
                    System.exit(0);
                }
                
            } // while end
        } catch (Exception e) {
            e.printStackTrace();
        } // try end
 
    }
 
}
 
cs

-switch문 다음에 이어질 코드(출력과 재실행 유무)들을 모두 while(true)에 넣음

-switch문 안에서 작성한 break;가 실행되면 while(true)가 아니라, switch를 빠져나오게 됨

-default: continue;가 실행되면, 다시 while문의 처음으로 올라가서 반복실행됨

 

Unreachable code 에러

  Unreachable code란? 도달하지 않는 구문, 절대 실행될 수 없는 구문 

  In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.[출처.위키백과]

en.wikipedia.org/wiki/Unreachable_code

 

Unreachable code - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the

en.wikipedia.org