import java.util.Scanner;
public class ScannerEx {
public static void main(String[] args) {
System.out.print("이름, 도시, 나이, 체중, 독신 여부를 빈칸으로 분리하여 입력하세요 : ");
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.print("이름 : " + name + ", ");
String city = sc.next();
System.out.print("도시 : " + city + ", ");
int age = sc.nextInt();
System.out.print("나이 : " + age + "살, ");
double weight = sc.nextDouble();
System.out.print("체중 : " + weight + "kg, ");
boolean single = sc.nextBoolean();
System.out.print("독신 여부 : " + single + "이다.");
sc.close();
}
입력
이름, 도시, 나이, 체중, 독신 여부를 빈칸으로 분리하여 입력하세요 : 미수 서울 17 50 True
출력
이름 : 미수, 도시 : 서울, 나이 : 17살, 체중 : 50.0kg, 독신 여부 : true이다.
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
스캐너를 사용하기 위한 코드다.
System.out.print("이름 : " + name + ", ");
이름 : 미수
처럼 출력을 하고 싶으면 "넣고 싶은 문자 : " + 변수
로 코드를 작성하면 된다.