-
백준 11654번 아스키코드Programming/BaekJoon 2021. 8. 20. 00:32
문자 한 개를 읽어 아스키코드 값을 출력하므로 굳이 스캐너까지 이용하지 않고 기초 입력 스트림을 이용한다.
import java.io.IOException; public class 아스키코드 { public static void main(String[] args) throws IOException { System.out.println(System.in.read()); } }
다른 방법 ) 스캐너
문자 하나를 읽어서 바로 아스키코드로 출력하면 되기 때문에 변수 선언을 안했다.
public class 아스키코드 { public static void main(String[] args) throws IOException { //System.out.println((int)new BufferedReader(new InputStreamReader(System.in)).readLine().charAt(0)); ...굳이? System.out.println((int)new Scanner(System.in).next().charAt(0)); //println()메서드는 매개변수를 받아 내부적으로 문자열로 처리하여 출력하므로 //A면 A, B면 B가 그대로 출력된다. //문자에 해당하는 아스키코드가 출력되어야 해서 int로 형변환을 명시해주었다. } }
변수 선언을 한다면
public class 아스키코드 { public static void main(String[] args) throws IOException { int a = new Scanner(System.in).next().charAt(0); // 자동형변환 System.out.println(a); } }
'Programming > BaekJoon' 카테고리의 다른 글
백준 8958번 OX퀴즈 (0) 2021.10.14 백준 1546번 평균 (0) 2021.08.11 백준 3052번 나머지 (0) 2021.07.16 백준 2577번 숫자의 개수 (0) 2021.07.16 백준 2562번 최대값 (0) 2021.07.16