最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java递归方法求5!的实现代码
时间:2017-04-24 编辑:简简单单 来源:一聚教程网
题目:利用递归方法求5!。
程序分析:递归公式:fn=fn_1*4!
程序设计:
代码如下 | 复制代码 |
importjava.util.Scanner; publicclassEx22 { publicstaticvoidmain(String[] args) { Scanner s =newScanner(System.in); intn = s.nextInt(); Ex22 tfr =newEx22(); System.out.println(tfr.recursion(n));
}
publiclongrecursion(intn) { longvalue =0; if(n ==1|| n ==0) { value =1; }elseif(n >1) { value = n * recursion(n-1); } returnvalue; }
} |
方法二利用递归方法求5!。
代码如下 | 复制代码 |
publicclasslianxi22 { publicstaticvoidmain(String[] args) { intn =5; rec fr =newrec(); System.out.println(n+"! = "+fr.rec(n)); } } classrec{ publiclongrec(intn) { longvalue =0; if(n ==1) { value =1; }else { value = n * rec(n-1); } returnvalue; } } |
-
下一个: 实例:Ionic2调用本地SQlite
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24