hyeonga_code
Java_18_표준 입출력 본문
-- 표준 입출력
- Scanner 코딩테스트에 사용하지 않습니다.
- utility입니다.
- 근원지에서 목적지로 데이터를 입력하여 전송합니다.
- 패킷_Packet 1byte로 지정합니다.
-- Stream : 근원지, 목적지에서 데이터를 내보내고 받아들이는 게이트를 의미
-- Byte Stream
- Input Stream : 데이터를 받아올 때
- Output Stream : 데이터를 전송할 때
- 한글을 전송하는 경우 오류가 발생
-- Character Stream
- 문자를 보내기 위함입니다.
- 2 Byte
- Reader
- Writer
-- 출력
- System.out/err
- print
- println
- printf
- write
-- out : 일반 메세지 출력
-- err : 에러 메세지 출력
- 1byte 를 읽어들입니다.
- Syste m.in.read()
- 아스키코드_ASCI Code
- -48,-'0'을 제거해야 원래 문자를 읽어들일 수 있습니다.
- 0 부터 9 까지 읽어들일 수 있습니다.
- BuffreReader br = new BufferReader(new InputStreamReader(System.in))
String str = br.readLine();
- String 으로 Enter를 입력받을 때까지 입력한 값을 저장합니다.
=====
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
|
package basic;
public class InputOutput {
public static void main(String[] args) {
System.out.print("Java Study Class ");
// 줄 바꿈 없이 출력
System.out.println("In March"); // Java Study Class In March
// 줄 바꿈 실행 됨
System.out.println("Wellcome!"); // Wellcome!
int a = 10;
int b = 20;
System.out.printf("%d + %d = %d \n", a, b, a + b); // 10 + 20 = 30
/* printf()
- 내가 지정한 양식으로 출력 가능
- 서식 문자
- %d : 정수형
- %f : 실수형
- %c : 문자형/char
- %s : 문자열
- %% : '%' 출력
- 특수 문자
- \n : 줄 바꿈/개행
- \t : 탭 크기만큼 띄어쓰기
- \\ : '\' 출력
*/
}
}
|
cs |
- 입력 : in
- 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
|
package basic;
import java.util.Scanner;
public class InputOutput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input your name: ");
String name = in.next();
// string 형 변수 name에 next로 문자열 값 입력 받아 저장
System.out.println("Your name is " + name + "."); // Your name is [name].
System.out.print("Input number: ");
int su = in.nextInt();
// int 형 변수 su에 nextInt로 정수 값을 입력 받아 저장
System.out.println("Number : " + su);
/* Int형 자료대신 String 자료형을 입력할 경우
Input number: word
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Exam_03.main(Exam_03.java:8)
*/
}
}
|
cs |
>Q. 이름, 국어점수, 영어점수를 입력받고 '이름'의 총점은 '총점'입니다.를 출력
=====
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
|
package basic;
import java.util.Scanner;
public class InputOutputEx {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
String name = in.next();
System.out.print("Kor: ");
int kor = in.nextInt();
System.out.print("Eng: ");
int eng = in.nextInt();
// 문자열에서의 '+'는 연결을 의미
System.out.println(name + "'s total score : " + kor + eng);
// [name]'s total score : [kor][eng]
// 숫자의 연산을 위해 ()필요 _ 연산자 우선 순위
System.out.println(name + "'s total score : " + (kor + eng));
// [name]'s total score : [kor+eng]
int tot = kor + eng;
System.out.printf("%s's total score : %d \n", name, tot);
// [name]'s total score : [tot]
/*
Name: 홍길동
Kor: 60
Eng: 80
홍길동's total score : 6080
홍길동's total score : 140
홍길동's total score : 140
*/
}
}
|
cs |
'Java' 카테고리의 다른 글
Java_20_연산자_단항 연산자 (0) | 2023.09.18 |
---|---|
Java_19_음수가 나오는 이유, 1의 보수, 2의 보수 (0) | 2023.09.17 |
Java_17_문자 데이터 문자, 문자열 (0) | 2023.09.16 |
Java_16_Wrapper Class 래퍼클래스, 언박싱, 박싱 (0) | 2023.09.16 |
Java_15_형 변환 Type Casting (0) | 2023.09.15 |