11-1. 입력과 출력
지난번 작성했던 코드에서
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;
public class OkJavaGoinHome {
public static void main(String[] args) {
String id = "JAVA APT 507";
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
}
}
변수를 매번 바꾸지 않고 필요한 사용자가 입력할수 있게 만들려면?
구글 검색기능을 활용한다.
- java popup input text swing 이라는 검색을 했을때
JOptionPane.showInputDialog("Enter a ID");
결과가 나온다. 위의 내용을 입력해주면 JOptionPane에 빨간줄이 생기게 되고 import 해주면 자동으로 맨윗줄에 추가가 된다.
Run as를 눌러주면
input 팝업창이 뜨게 된다! wow!
숫자를 입력했을때 불밝기를 변경하고 싶다면?
import javax.swing.JOptionPane; //javax.swing 불러온다
import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;
public class OkJavaGoinHomeinput {
public static void main(String[] args) {
String id = JOptionPane.showInputDialog("Enter a ID");
String bright = JOptionPane.showInputDialog("Enter a Bright level");
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
//밝기를 조절할수 있는 램프 추가
DimmingLights moodLamp = new DimmingLights(id+" moodLamp");
moodLamp.setBright(Double.parseDouble(bright)); // bringt를 넣으면 에러가 뜬다. double형을 넣어줘야하는데 위에는 string으로 선언했기 때문! 그래서 Double.parseDouble(bright)를 넣어준다.
moodLamp.on();
}
}
string으로 선언되어있는 불밝기를 double 형으로 변경시켜주는 코드
Double.parseDouble(bright) fmf setBright 안에 넣어준다!
Java APT 102-1111 -> Elevator callForUp stopFloor : 1
Java APT 102-1111 -> Security off
Java APT 102-1111 / Hall Lamp -> Lighting on
Java APT 102-1111 / floorLamp -> Lighting on
Java APT 102-1111 moodLamp -> DimmingLights bright : 5.0
Java APT 102-1111 moodLamp -> Lighting on
이렇게 결과값이 나온다.
'java' 카테고리의 다른 글
catch TypeError: Cannot read properties of undefined 해결하기 (0) | 2023.04.28 |
---|---|
11-2~12. 입력과 출력, 직접 컴파일 (0) | 2023.01.23 |
쉽게배우는 자바 9-1~10 (0) | 2023.01.21 |
쉽게 배우는 자바 8-2~8-3 변수, 데이터 타입 변환 (0) | 2023.01.21 |
왜 자바인가? (0) | 2023.01.18 |