입출력 이해하기

1. 다음 코드를 작성하여 실행 한 후, 콘솔에 12 34 65를 입력해서 출력을 확인해보세요.

 

Scanner sc = new Scanner(System.in);

int input = sc.nextInt();

System.out.println(input);

 

2. 위의 입 출력 동작을

System.in.read, System.out.write, System.out.print, System.out.flush와

변수를 자유롭게 이용해서 똑같이 구현해보세요.

즉, 4개의 함수를 이용해서

12 34 65 를 입력하면, (1)과 똑같이 출력이 나오게 구현하세요.

반복문은 사용하지 말구요!

 


✅첫번째 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int input = System.in.read();
int input2 = System.in.read();
int input3 = System.in.read();
int input4 = System.in.read();
int input5 = System.in.read();
int input6 = System.in.read();
int input7 = System.in.read();
int input8 = System.in.read();
 
System.out.write(input);
System.out.write(input2);
System.out.write(input3);
System.out.write(input4);
System.out.write(input5);
System.out.write(input6);
System.out.write(input7);
System.out.write(input8);
System.out.flush();
cs

 

✅결과

 


✅두번째 방법

1
2
3
4
5
6
7
8
9
System.out.write(System.in.read());//write는 byte코드를 입력받으면 시스템 기존값으로 변경해줌!
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.flush();
cs

 

✅결과

 

 

+ Recent posts