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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Method { public static void main(String[] args) { //재귀 메소드 Recursive Method // - 메소드가 자기 자신을 호출하는 메소드 //test(); //팩토리얼 //4! = 4 x 3 x 2 x 1 //4! = 24 int n = 4; int result = factorial(n); System.out.printf("%d = %d\n", n, result); } public static void test() { System.out.println("테스트"); test(); //***재귀호출 } public static int factorial(int n) { if (n == 1) { return 1; } else { return n * factorial(n -1); } } } | cs |
'WEB > JAVA' 카테고리의 다른 글
날짜&시간1 (0) | 2015.05.22 |
---|---|
메소드6 (예제) (0) | 2015.05.22 |
메소드4 (메소드 오버로딩) (0) | 2015.05.22 |
메소드3 (지역변수) (0) | 2015.05.22 |
메소드2 (구성요소) (0) | 2015.05.22 |