java遞歸實現階乘factorial

public class jiecheng {
	static int result=1;
	
	public static int jiecheng(int n) {
		
		if (n==1) {
			
			return 1;
		}
		else
			return n*jiecheng(n-1);
		
//		if (n>0) {
//			result = result*n;
//			
//			n--;
//			
//			jiecheng(n);
//		}else {
//			return result;
//		}
		
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc =  new Scanner(System.in);
		
		int a = sc.nextInt();
		
//		System.out.println(a);
		int b = jiecheng(a);
		
		System.out.println(b);
		
		sc.close();

	}

}

 

發佈了27 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章