JAVA 메소드 - 메소드의 출력

황찬호·2021년 4월 12일
0

Java1

목록 보기
13/14

메소드는 입력값이 있고, 그것을 처리해서 출력해준다.

출력을 위해 사용하는 핵심 키워드는 return이고 return시 어떤 데이터 타입으로 출력시킬지 입력해주어야 한다.

return 값을 가지고 있는 메소드는 메소드의 재사용성이 높아진다.


public class OutputMethod {
	
	public static String a() {
		// ...
		return "a";
	}
	
	public static int one() {
		return 1;
	}

	public static void main(String[] args) {
		
		System.out.println(a());
		System.out.println(one());

	}

}
import java.io.FileWriter;
import java.io.IOException;

public class WhyMethod {
     
    public static void main(String[] args) throws IOException {
        
    	System.out.println(twoTimes("++", "a"));
    	FileWriter fw = new FileWriter("out.txt"); // FileWriter 후 파일이 안보이면 리프레시 해주기
    	fw.write(twoTimes("*", "a"));
    	fw.close();
    	//email.send("chanho@abc.com"); // 실제론 동작하지 않지만 있다면 예를 들면 이런식이다.
    	
    }
                                     // 매개변수, parameter
    public static String twoTimes(String delimiter, String text) {
    	String out = ""; // "" <- String은 + 를 이용해서 문자열을 합쳐야 하기 때문
    	out = out + "---" + delimiter + "---" + "\n";
    	out = out + "abc" + text + "\n";
    	out = out + "abc" + text + "\n";
    	return out;
    }
 
}
profile
되는대까지 해보기

0개의 댓글