利用递归方法求10!
- public class Test {
- public static void main(String[] args) {
- int n = 10;
- rec fr = new rec();
- System.out.println(n+"! ="+fr.rec(n));
- }
- }
- class rec{
- public long rec(int n) {
- long value = 0 ;
- if(n ==1 ) {
- value = 1;
- } else {
- value = n * rec(n-1);
- }
- return value;
- }
- }
复制代码 |
|