package com.kh.variable;
import java.util.Scanner;
public class B_KeyboardInput {
// 사용자가 키보드로 입력한 갓을 가져오고자 할 때 사용할 클래스
// java.util.Scanner 클래스에서 제공하는 메소드를 통해 입력한 값을 가져올 수 있음.
public void inputScanner1() {
// 사용자의 인적사항을 입력받은 후 출력
// 클래스 생성
Scanner sc = new Scanner(System.in);
System.out.print("당신의 이름은 무엇입니까: ");
// 사용자가 입력한 값을 문자열로 받고자 할 때 사용되는 메소드 ( sc.next(), sc.nextLine() )
String name = sc.nextLine(); // name이라는 박스가 먼저 만들어지고 sc.next 메소드를 실행 그다음 연산자(=) 실행
// (1) (3) (2) --> 항상 이 순서로 실행된다. 대입연산자(=) 를 기준으로 왼쪽이 먼저 그다음 오른쪽
// sc.next() : 공백 이전까지 값만 가져옴
// sc.nextLine() : 한 줄단위로 통째로 가져옴
System.out.print("당신의 나이는 몇살입니까: "); // 사용자가 엔터치는 순간 버퍼라는 공간에 정보가 들어왔을 것
// 사용자가 입력한 값을 정수값으로 가져오고자 할 때 ( sc.nextInt() )
int age = sc.nextInt();
System.out.print("당신의 키는 몇입니까(cm단위로 소수점 첫째 자리까지 입력하시오): ");
// 사용자가 입력한 값을 실수값으로 가져오고자 할 때 (sc.nextDouble())
double height = sc.nextDouble();
// 홍길동님은 19세 이며, 162.3cm입니다.
System.out.println(name + "님은 " + age + "세 이며, " + height + "cm입니다.");
}
public void inputScanner2() {
Scanner sc = new Scanner(System.in);
System.out.print("이름 : ");
String name = sc.nextLine();
System.out.print("나이 : ");
int age = sc.nextInt();
/*
* sc.nextLine() : 버퍼에서 '엔터'까지의 모든 값을 가져옴 (즉, 엔터가비워짐)
* 그 외의 메소드 : 버퍼에서 '엔터' 이전까지의 값만 가져옴 (즉, 엔터를 비워주지 않음)
*/
sc.nextLine();
System.out.print("주소 : ");
String address = sc.nextLine();
System.out.print("키 : ");
double height = sc.nextDouble();
//ㅇㅇㅇ님은 ㅇㅇ살이며, 사는 곳은 000이고, 키는 000cm 입니다.
System.out.println(name + "님은 " + age + "살이며, 사는 곳은 " +
address + "이고, 키는 " + height + "cm 입니다.");
System.out.printf("%s님은 %d살이며, 사는 곳은 %s이고, 키는 %.2fcm입니다.", name, age, address, height);
//결론 sc.nextOOO() 메소드 뒤에 nextLine()가 와야되는 경우
// 버퍼에 남아있는 '엔터'를 비워주기 위해 사이에 sc.nextLine()를 호출해주자
}
public void inputScanner3() {
Scanner sc = new Scanner(System.in);
// 문자열을 입력받을 때 => sc.nextLine();
// 정수값을 입력받을 때 => sc.nextInt();
// 실수값을 입력받을 때 => sc.nextdouble();
System.out.print("이름: ");
String name = sc.nextLine();
System.out.print("성별(M/F) : ");
char gender = sc.nextLine().charAt(0);
// "Male".charAt(0) --> 'M' M이 0번 인덱스, a가 1번 인덱스 ...
System.out.print("나이: "); // 20\n
int age = sc.nextInt(); //int age = 20;
sc.nextLine();
System.out.print("주소 : ");
String address = sc.nextLine(); //String address= "";
System.out.println(name + "님의 개인정보");
System.out.println("성별: " + gender);
System.out.println("나이: " + age);
System.out.println("주소: " + address);
}
}
Point
- Scanner : 사용자로 부터 입력 값을 받기 위해 사용하는 class
사용법
1. 메소드 안에 Scanner sc = new Scanner(System.in); 호출
2. import.java.util.Scanner; 생성
3. sc.next(), sc.nextLine() 메소드 생성
- sc.nextLine : 버퍼에서 \n(엔터) 까지의 모든 값을 가져옴.
그 외 메소드 : 버퍼 이전까지의 모든 값을 가져옴.
sc.nextInt(); 다음 sc.nextLine();을 쓸 경우 중간에 sc.nextLine();를 한번 출력해서 버퍼에
남아 있는 \n(엔터)를 비워주어야함
'JAVA > JAVA 기초' 카테고리의 다른 글
JAVA 기초 - 상수(Constant) (0) | 2020.05.28 |
---|---|
Java 기초 - Overflow (0) | 2020.05.28 |
Java 기초- 형변환 (Type Casting) (0) | 2020.05.28 |
JAVA 기초 - Variable (변수) (0) | 2020.05.25 |
JAVA 기초 - 프로그래밍 (0) | 2020.05.24 |