- BufferedReader를 사용하지 않고 숫자를 입력하는 방법
- InputStreamReader + BufferedReader 조합에 비해 간단하지만 속도가 느려질 수 있어 대용량 데이터 처리에는 부적합.
1.Scanner()클래스를 통해 숫자 or 문자열 입력받기
package org.example;
import java.util.Scanner;
public class ScannerEx {
public void readTwoNumbersAndPlus(){
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt()+sc.nextInt()); //숫자형
// System.out.println(sc.next()+sc.next()); //문자형
// System.out.println(sc.nextFloat()+sc.nextFloat()); //소수점6자리까지
// System.out.println(sc.nextDouble()+sc.nextDouble()); //소수점6자리까지
}
}
package org.example;
import java.util.Scanner;
public class ScannerEx {
public void readTwoNumbersAndPlus(){
Scanner sc = new Scanner(System.in);
System.out.println("두 개의 숫자를 입력하세요.");
System.out.print("첫번째 숫자:");
System.out.println("첫번째 숫자는" +sc.nextInt()+ " 를 입력했습니다");
System.out.print("두번째 숫자:");
System.out.println("두번째 숫자는" +sc.nextInt()+ " 를 입력했습니다");
System.out.println(sc.nextInt()+sc.nextInt());
}
}
2.Main() 클래스
package org.example;
public class ScannerExTest {
public static void main(String[] args) {
ScannerEx scannerEx = new ScannerEx();
scannerEx.readTwoNumbersAndPlus();
}
}