public static void keyPressExample() throws Exception {}
throws Exception 코드 중에 키보드 또는 마우스 입력값을 받아 실행하는 코드일 경우 JVM밖에서 일어나는 에러를 처리하기 위한 선언
System.in.read(); 사용 시 자동생성
boolean run = true;
int temperature = 30;
int keycode;
System.out.println("------------------------------");
System.out.println("온도를 설정하시려면 버튼을 눌러주세요");
System.out.println("1. 온도 낮춤 |2. 온도 높임 ");
System.out.println("3. 18도 설정 |4. 온도설정 종료");
System.out.println("------------------------------");
while(run) {
keycode = System.in.read();
if(keycode!=13 && keycode!=10) {
if(keycode == 49) {
if(temperature != 18) {
temperature--;
System.out.println("냉방 "+temperature+"도로 설정하였습니다.");
} else if(temperature == 18) {
System.out.println("최저온도 "+temperature+"도로 내릴 수 없습니다.");
}
} else if(keycode == 50) {
temperature++;
System.out.println("냉방 "+temperature+"도로 설정하였습니다.");
} else if(keycode == 51) {
temperature = 18;
System.out.println("최저온도 "+temperature+"도로 설정하였습니다.");
} else if(keycode == 52) {
run = false;
}
}
}
System.out.println("온도설정 완료!");
값을 입력할 때 Enter를 같이 누르므로 Enter(13,10)를 제외할 조건 필요
System.in.read()에서 전달받은 코드가 모두 전달되어 비워질 때까지 while문 실행
run = false; 를 사용하여 while문 종료(break;사용 가능)