○ 콘솔창에 출력하기 (참고 : https://binscode.tistory.com/74)
① System.out.print(출력할 내용); → 콘솔창에 그냥 출력
② System.out.println(출력할 내용); → 콘솔창에 출력하고 줄바꿈
③ System.out.printf("출력서식", 값); → 출력서식을 이용해서 콘솔창에 출력
○ 출력 서식
- \n (역슬래쉬n) : 엔터, 줄 바꿈
- \t (역슬래쉬t) : 탭, 일정 간격 띄우기
System.out.print("사과\n참외\n수박\n\n\n");
System.out.print("바\t\t나\n나");
System.out.print("\n딸기\n");
- 형식) System.out.printf("출력서식", 값);
① %d : 10진 정수형
System.out.printf("%d %d %d \n", 3, 5, 7);
System.out.printf("나이: %d \n", 3);
System.out.printf("a: %d b: %d c: %d\n", 2, 4, 6);
- 출력서식 앞에 숫자가 붙으면, 할애할 출력 칸수
System.out.printf("%5d", 123); //" 123" : 5칸을 주고 123을 쓰는데, 앞쪽 빈칸은 공백으로 채운다
System.out.printf("%-5d", 123); //"123 " : 5칸을 주고 123을 쓰는데, 뒤쪽 빈칸은 공백으로 채운다
System.out.printf("%05d", 123); //"00123" : 5칸을 주고 123을 쓰는데, 앞쪽 빈칸은 0으로 채운다
② %f : 실수형
System.out.printf("x:%f y:%f z:%f", 1.2, 3.4, 5.6);
System.out.printf("%6.2f", 7.8); //" 7.80" : 6칸을 주고 7.8을 쓰는데, 소수점 2자리로, 앞 빈칸은 공백
System.out.printf("%-6.2f", 7.8);//"7.80 " : 6칸을 주고 7.8을 쓰는데, 소수점 2자리로, 뒤 빈칸은 공백
System.out.printf("%.2f", 7.8); //"7.80" : 7.8을 쓰는데, 소수점은 2자리로 쓴다
③ %c : 문자형
System.out.printf("%c %c %c", 'S', 'K', 'Y');
System.out.printf("%5c", 'R'); //" R" : 5칸 잡고 오른쪽 정렬
System.out.printf("%-5c", 'r'); //"r " : 5칸 잡고 왼쪽 정렬
④ %s : 문자열형
System.out.printf("%s %s %s", "Year", "Month", "Date");
System.out.printf("%8s", "HAPPY"); //" HAPPY" : 8칸 잡고 오른쪽 정렬
System.out.printf("%-8s\n", "HAPPY"); //"HAPPY " : 8칸 잡고 왼쪽 정렬
System.out.println(String.format("%.4f", 10/3.0)); //"3.3333" (String클래스의 함수 이용)
'Backend > JAVA_Basic' 카테고리의 다른 글
07. JAVA, 제어문 (반복문, 조건문, for, while, if, switch) & 연습문제 (0) | 2022.05.29 |
---|---|
06. JAVA, Math 클래스 (난수, 올림, 버림, 반올림 등 ) (0) | 2022.05.28 |
04. JAVA 기본문법 (자료형, 형변환, Wrapper 클래스) (0) | 2022.05.27 |
03. JAVA 기본문법 (식별자, 콘솔창 출력, 연산자) (0) | 2022.05.27 |
02. Eclipse IDE for Enterprise Java and Web Developers 설치 (0) | 2022.05.26 |
댓글