티스토리 뷰

Java(생활코딩)

[Java] 입력과 출력

codeyun2 2023. 1. 3. 00:01

프로그램: 입력(Input)을 받아 출력(Output)하는 것

입력에 따라 프로그램이 다르게 동작함

Input: Argument, File, Network, Audio, Program

Output: Monitor, File, Audio, Program

 

 

인풋으로 값 받기

① 인풋창으로 값을 받기 위해 javax.swing.JOptionPane 임포트

showInputDialog 메서드로 인풋창을 띄움

- 전달인자로 어떤 정보를 받을 건지 설명을 적음(인풋 문자열 -> 아웃풋 문자열)

 

② 인풋창으로 받은 두번째 값 bright를 문자열에서 double 타입으로 바꿔주기 위해 Double.parseDouble(바꿀값) 사용

 

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 OkJavaGoingHomeInput {
    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)); // ② 
        moodLamp.on();
    }
}

 

매개변수로 값 받기

// 입력: "args": ["JAVA APT", "50"]
public class OkJavaGoingHomeInput {
    public static void main(String[] args) { // 문자열 배열을 받을 건데 그걸 args라고 부를 것임
        String id = args[0];
        String bright = args[1];
        // 생략
}

 

 

vscode

자동 import: alt shift O

 

java 파일 실행 시 인자 전달하기

: 실행 및 디버그의 톱니바퀴(launch.json)에 들어가 해당파일에 "args":[전달인자1, 전달인자2]를 추가해준다

=> 기존 객체를 복사하여 특정한 name으로 바꿔주면(주로 파일이름+전달인자) 같은 값으로 계속해서 테스트할 수 있음

{
  "type": "java",
  "name": "Launch OkJavaGoingHomeInput",
  "request": "launch",
  "mainClass": "OkJavaGoingHomeInput",
  "projectName": "vscode_Java_39c2b6f9"
},
{
  "type": "java",
  "name": "Launch OkJavaGoingHomeInput 'JAVA APT', '500'",
  "request": "launch",
  "mainClass": "OkJavaGoingHomeInput",
  "projectName": "vscode_Java_39c2b6f9",
  "args": ["JAVA APT", "50"]
},
{
  "type": "java",
  "name": "Launch OkJavaGoingHomeInput 'CUP APT', '10'",
  "request": "launch",
  "mainClass": "OkJavaGoingHomeInput",
  "projectName": "vscode_Java_39c2b6f9",
  "args": ["CUP APT", "10"]
}

 

 

 

 

 

2023.01.02
생활코딩 JAVA1 11

'Java(생활코딩)' 카테고리의 다른 글

[Java] 디버거  (1) 2023.01.01
[Java] Programming  (0) 2022.12.31
[Java] 변수  (0) 2022.12.30
[Java] 데이터 타입  (0) 2022.12.28
[Java] 설치  (0) 2022.12.26
댓글
공지사항