1、递归方法:
- public class DiGui {
- public static void main(String[] args) {
- System.out.println(f(5));
- }
- public static long f(int n) {
- if(n <= 1) {
- return 1;
- } else {
- return n * f(n-1);
- }
- }
- }
2、非递归:
- public class WuJie {
- public static void main(String[] args) {
- long result = 1L;
- for(int i=1; i<=5; i++) {
- result*=i;
- }
- System.out.println(result);
- }
- }