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("요즘 너무 춥네요!");
}
}
'언어 > JAVA' 카테고리의 다른 글
메소드_정수형 변수 2개로 문자형 변수(+, -, *, /)를 이용해 연산하기 (0) | 2023.03.04 |
---|---|
메소드_덧셈 뺄셈 나눗셈 곱셈이 가능한 메소드 작성 (0) | 2023.03.04 |
Sequential Search(순차탐색)과 Binary Search(이진탐색) (0) | 2023.03.04 |
SelectionSort (선택정렬) (0) | 2023.03.04 |
BubbleSort (버블정렬) (0) | 2023.03.03 |
댓글