JAVA/Method
[JAVA-Eclipse] Method - 2
삐옥
2021. 12. 10. 00:31
매개변수의 개수와 전달하는 값의 개수는 반드시 일치해야 한다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class SelfStudy {
public static void main(String[] args) {
m("삐옥이", 6);
}
private static void m(String name, int age) {
String result = age >= 19? "성인": "미자";
System.out.printf("%s님은 %s입니다.", name, result);
}
}
|
cs |
void m( ) : Method Signature, 메소드 서명(Sign)
public static void m( ) { }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class SelfStudy {
public static void main(String[] args) {
System.out.println(m(1));
}
public static boolean m(int a) {
a = a + 1;
a *= 2;
a -= 3;
boolean result = a > 0? true : false;
return result;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static void main(String[] args) {
String result = m(10)? "양수" : "음수";
System.out.println(result);
}
public static boolean m(int a) {
a = a + 1;
a *= 2;
a -= 3;
boolean result = a > 0? true : false;
return result;
}
}
|
cs |
Recursive Method 재귀 메소드
- 메소드가 구현부에서 자기 자신을 호출하는 구문을 가지는 메소드
- 동일한 형태의 메소드(=자기자신)를 반복 호출하기 때문에 다루기가 어려움
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class SelfStudy {
public static void main(String[] args) {
int a = 4;
int result = factorial(a);
System.out.printf("%d! = %d\r\n", a, result);
}
//재귀 메소드
private static int factorial(int a) {
if(a == 1) {
return 1;
} else {
return a * factorial(a-1); //재귀 호출
}
}
}
|
cs |