java 求阶乘

package com.oop;

public class factorical {
	public static void main(String[] args) {
		long result = sumOf(10);
		System.out.println(result);
		
		long sum = sum(10);
		System.out.println(sum);
	}
	
	public static long sumOf(int n) {
		if (n == 1 || n == 0) {
			return 1;
		}
		else {
			return n*sumOf(n-1);
		}
	}
	
	public static long sum(int n) {
		long total = 1;
		for(int i = 1; i <= n;i++) {
			total *= i;
		}
		return total;
	}
}
posted @ 2022-06-20 08:41  wjxuriel  阅读(38)  评论(0)    收藏  举报