보리차
chapter 12 콘솔 입력과 출력 본문
콘솔 출력(Console Output)
System.out.println()
System.out.print()
문자열 조합
System.out.printf("정수는 %d, 실수는 %f, 문자는 %c", 12, 24.5, 'A');
콘솔 입력(Console Input)
Scanner 클래스
생성자로 전달되는 대상으로부터 데이터를 추출하는 기능을 제공.
파일이나 String 인스턴스 등 다양한 대상으로부터의 데이터 추출이 가능하다.
import java.util.Scanner;
class ScanningString {
public static void main(String[] args) {
String source = "1 3 5";
Scanner sc = new Scanner(source);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int sum = num1 + num2 + num3;
System.out.printf("%d + %d + %d = %d \n", num1, num2, num3, sum);
}
}
Scanner클래스의 키보드 적용
Scanner sc = new Scanner(source);
-> Scanner sc = new Scanner(System.in);
키보드의 입력값을 받아올 수 있다.
Scanner 클래스의 주요 메소드들
int nextInt()
byte nextByte()
String nextLine()
double nextDouble()
boolean nextBoolean()
'Java' 카테고리의 다른 글
chapter 14 클래스의 상속 1: 상속의 기본 (0) | 2022.01.28 |
---|---|
chapter 13 배열(Array) (0) | 2022.01.28 |
chapter 11 메소드 오버로딩과 String 클래스 (0) | 2022.01.26 |
chapter 10 클래스 변수와 클래스 메소드 (0) | 2022.01.26 |
chapter 09 정보 은닉 그리고 캡슐화 (0) | 2022.01.25 |