본문 바로가기

Java 공부

7. Java에서 Scanner로 여러 유형의 값 입력 받기 (byte, short, int, long, float, double)

 

클래스를 하나 만들어줍시다.

 

내용은 이렇게 입력해주세요

import java.util.Scanner;

public class ScannerTest {

public static void main(String[] args) {
Scanner POWERSCAN = new Scanner(System.in);

byte b;
short s;
int i;
long l;

float f;
double d;


System.out.print("byte변수에 담길 값: ");
b = POWERSCAN.nextByte();

System.out.print("short변수에 담길 값: ");
s = POWERSCAN.nextShort();

System.out.print("int변수에 담길 값: ");
i = POWERSCAN.nextInt();

System.out.print("long변수에 담길 값: ");
l = POWERSCAN.nextLong();

System.out.print("float변수에 담길 값: ");
f = POWERSCAN.nextFloat();

System.out.print("double변수에 담길 값: ");
d = POWERSCAN.nextDouble();


System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
}
}

 

 

결과:

 

잘 실행되었습니다.