재귀호출

|
public class FactorialTest {
	public static void main(String[] args) {
		System.out.println(factorial(3));
		System.out.println(factorialTri(5));
	}
	static long factorial(int n){
		long result = 0;
		if(n == 1) {
			result = 1;
		}
		else {
			result = n * factorial(n-1);
		}
		return result;
	}
	
	static long factorialTri(int n) {
		return (n==1) ? 1 : n * factorialTri(n-1);
	}
}

'오라클자바커뮤니티 > 자바' 카테고리의 다른 글

JVM 메모리 구조  (0) 2014.10.12
메서드의 사용  (0) 2014.10.12
메서드의 정의 및 작성  (0) 2014.10.12
클래스의 정의  (0) 2014.10.12
클래스의 이해  (0) 2014.10.12
And