JAVA/생활코딩 JAVA 입문 수업

JAVA1 - 11.2. 입력과 출력 : arguments & parameter

kms152000 2023. 3. 21. 17:45

1. 실행버튼 아래에 Run configuraions의 쓰임: 변수의 값을 지정할 때 사용자로부터 직접 입력받지 않아도 됩니다. configurations에 arguments에 변수의 값을 써 놓으면, 써놓은 순서대로 args 배열에 저장이 됩니다. 예를 들어, arguments에 "Pusan APT 507", "15.0" 이렇게 쓰고 Apply를 누르면 자동으로 args[0]에는 "Pusan APT 100"이, args[1]에는 "15.0"이 저장됩니다.

 

2. Public static void main(String[] args)할 때 이 args기 때문에 따로 선언할 필요 없습니다. 사용할 변수에 args를 넣어주면 됩니다. 예를 들어, id 변수와 bright 변수를 사용하고 싶으면, String id = args[0], String bright = args[1] 이렇게만 해주면 됩니다.

 

 

import javax.swing.JOptionPane;

import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;

public class OkJavaGoInHomeInput {

	// parameter, 매개변수
	public static void main(String[] args) {
		
		String id = args[0];
		String bright = args[1];
				
		// 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));
		moodLamp.on();
	}

}