메시지 입력받기 : Scanner
Scanner.in 객체의 next()명령을 사용해서 입력
1. 사용자로부터 정보를 읽어오는 객체 Scanner 생성
import java.util.Scanner;
public class Main {
public static void main(String[] args){
// Scanner 객체를 new 명령어로 생성
Scanner sc = new Scanner(System.in);
}
}
2. Scanner 객체를 이용해 변수에 데이터 입력 받기
- 사용자가 입력한 문자열
- 엔터치기 전 공백과 엔터는 포함되지 않음
- 스페이스바 감지
String input = sc.next();
String input = sc.nextLine();
char input = sc.next().charAt(0);
byte input = sc.nextByte();
short input = sc.nextShort();
int input = sc.nextInt();
long input = sc.nextLong();
float input = sc.nextFloat();
double input = sc.nextDouble();
- 주의사항
nextInt(), nextfloat()를 사용하고 next(), nextLine()을 쓸 경우 nextInt()와 nextfloat()를 사용한 후에는 개행문자('\0') 가 버퍼에 남아있기 때문에 scanner.nextLine()을 통해 Queue에 남은 값들을 비워줘야 한다.
메시지 출력하기 : System.out.print() & println()
괄호( ) 안의 데이터를 출력하는 방법
System.out.print("문자열");
괄호( ) 안의 데이터를 출력하고 줄바꿈을 해주는 방법
System.out.println("문자열");