Eggs Sunny Side Up
본문 바로가기
언어/JAVA

메소드_변수와 리턴의 존재여부

by guswn100059 2023. 3. 4.

1. 변수와 리턴이 존재하는 경우

package 메소드;

public class Ex01_변수와리턴_존재 {

	public static void main(String[] args) {
		
		sum(5, 10);

	}
	
	public static int sum(int a, int b) {
		int result = a + b;
		return result;
	}

}

2. 변수는 있고 리턴이 없는 경우

package 메소드;

public class Ex02_변수존재_리턴없음 {

	public static void main(String[] args) {


		//sumPrint 메소드를 이용해서 3과 5를 더한 값 출력하기
		
		//호출
		sumPrint(3, 5); 

	}
	
	public static void sumPrint(int a, int b) {
		System.out.println("두 수의 합은 "+(a+b)+" 입니다.");
	}

}

3. 변수가 없고 리턴이 있는 경우

package 메소드;

public class Ex03_변수없고_리턴있고 {

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

	}
	
	public static String getName() {
		return "박현주";
	}

}

4. 변수와 리턴 모두 없는 경우

package 메소드;

public class Ex04_변수와리턴X {
	
	public static void main(String[] args) {
		
		todayWeather();
		
	}
	
	public static void todayWeather() {
		System.out.println("요즘 너무 춥네요!");
	}

}

 

댓글