본문 바로가기

JAVA/JAVA

자바 예제 - 식별자와 예약어 : 사용자로부터 이름, 나이, 주소를 입력받아 정보를 출력하는 프로그램을 작성

반응형

사용자로부터 이름, 나이, 주소를 입력받아 정보를 출력하는 프로그램을 작성해보기


import java.util.Scanner;


class information{


public static void main(String args[]){


String name, address;

int age;


Scanner sc = new Scanner(System.in);


System.out.println("당신의 이름을 입력하세요.");

name = sc.next();

System.out.println("당신의 나이를 입력하세요.");

age = sc.nextInt();


sc.nextLine();


System.out.println("당신의 주소를 입력하세요.");


address = sc.nextLine();

//address = sc.next();


System.out.println("이름 : " + name);

System.out.println("나이 : " + age);

System.out.println("주소 : " + address);

}

}


** sc.nextLine();

** 현재 쓰고 있는 sc.next(); 는 예) "경기도 수원" 같은 띄어쓰기를 수행하지 못함

그래서 sc.nextLine(); 을 사용하여 위에 것이 가능하도록 함

** nextLine() 은 다른 것과 동작방식이 다르다 !!!

next(), nextInt(), nextDouble() 엔터와, 스페이스로 구별

nextLine() 은 엔터로만 구별

그래서 nextLine()을 써준다. 동작방식이 다른 것과 경계되는 부분에 써준다.

반응형