JAVA chapter18. IO 기반 입출력 및 네트워킹. 


18.3 콘솔 입출력


18.3.1 System.in 필드


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
package sec03.exam01_system_in_out;
 
import java.io.InputStream;
 
public class SystemInExample1 {
    public static void main(String[] args) throws Exception {        
        System.out.println("== 메뉴 ==");
        System.out.println("1. 예금 조회");
        System.out.println("2. 예금 출금");
        System.out.println("3. 예금 입금");
        System.out.println("4. 종료 하기");
        System.out.print("메뉴를 선택하세요: ");
        
        InputStream is = System.in;
        char inputChar = (char) is.read();            
        switch(inputChar) {
            case '1':
                System.out.println("예금 조회를 선택하셨습니다.");
                break;
            case '2':
                System.out.println("예금 출금를 선택하셨습니다.");
                break;
            case '3':
                System.out.println("예금 입금를 선택하셨습니다.");
                break;
            case '4':
                System.out.println("종료 하기를 선택하셨습니다.");
                break;
        }            
    }
}
cs





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
package sec03.exam01_system_in_out;
 
import java.io.InputStream;
 
public class SystemInExample1 {
    public static void main(String[] args) throws Exception {        
        System.out.println("== 메뉴 ==");
        System.out.println("1. 예금 조회");
        System.out.println("2. 예금 출금");
        System.out.println("3. 예금 입금");
        System.out.println("4. 종료 하기");
        System.out.print("메뉴를 선택하세요: ");
        
        InputStream is = System.in;
        char inputChar = (char) is.read();            
        switch(inputChar) {
            case '1':
                System.out.println("예금 조회를 선택하셨습니다.");
                break;
            case '2':
                System.out.println("예금 출금를 선택하셨습니다.");
                break;
            case '3':
                System.out.println("예금 입금를 선택하셨습니다.");
                break;
            case '4':
                System.out.println("종료 하기를 선택하셨습니다.");
                break;
        }            
    }
}
cs



18.3.2 System.out 필드

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
package sec03.exam01_system_in_out;
 
import java.io.OutputStream;
 
public class SystemOutExample {
    public static void main(String[] args) throws Exception {
        OutputStream os = System.out;        
        
        for(byte b=48; b<58; b++) {
            os.write(b);
        }        
        os.write(10);
        
        for(byte b=97; b<123; b++) {
            os.write(b);
        }        
        os.write(10);        
 
        String hangul = "가나다라마바사아자차카타파하";
        byte[] hangulBytes = hangul.getBytes();
        os.write(hangulBytes);
        
        os.flush();
    }
}
cs

 


18.3.3 Console 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package sec03.exam02_console;
 
import java.io.Console;
 
public class ConsoleExample {
    public static void main(String[] args) {
        Console console = System.console();
        
        System.out.print("아이디: ");
        String id = console.readLine();
        
        System.out.print("패스워드: ");
        char[] charPass = console.readPassword();
        String strPassword = new String(charPass);
        
        System.out.println("---------------------");
        System.out.println(id);
        System.out.println(strPassword);
    }
}
cs




18.3.4 Scanner 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec03.exam03_scanner;
 
import java.util.Scanner;
 
public class ScannerExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("문자열 입력> ");
        String inputString = scanner.nextLine();
        System.out.println(inputString);        
        System.out.println();
        
        System.out.print("정수 입력> ");
        int inputInt = scanner.nextInt();
        System.out.println(inputInt);
        System.out.println();
        
        System.out.print("실수 입력> ");
        double inputDouble = scanner.nextDouble();
        System.out.println(inputDouble);
    }
}
cs




Posted by 너래쟁이
: