java练习笔记 2011.06.07
2011-06-08 15:53
162 查看
计算result=1!+...+10!
一、使用for循环
public class Factorial { public static void main(String[] args) { long result = 0; long temp = 1; for(int i=1; i<=10; i++) { temp *= i; result += temp; } System.out.println("result=" + result); } }
二、使用递归调用
public class Recursive { public static void main(String[] args) { long result = 0; for (int i=1; i<=10; i++) { long showResult = 0; showResult = factorial(i); System.out.println(i + "!=" + showResult); result += showResult; } System.out.println("1!+...+10!=" + result); } public static long factorial(int number) { if (number == 1) { return 1; } else { return number * factorial(number - 1); } } }
相关文章推荐
- JAVA学习笔记_打印图形练习
- Java基础知识强化之IO流笔记70:Properties练习之 如何让猜数字小游戏只能玩5次的案例
- 算法笔记_102:蓝桥杯练习 算法提高 快乐司机(Java)
- java学习笔记之String类及其练习
- java笔记--day09--接口(四)练习
- 算法笔记_062-蓝桥杯练习 最小乘积(基本型)(Java)
- 黑马程序员——Java练习笔记——final
- 黑马程序员_JAVA笔记24——网络编程(练习)
- 算法笔记_089:蓝桥杯练习 7-2求arccos值(Java)
- Java基础知识强化之IO流笔记56:IO流练习之 登录注册IO版
- 《黑马程序员》java笔记->关于String类及方法概述,练习
- (44)21.4.3 并发 练习 18---Java编程思想之并发笔记
- Java 基础一些代码练习笔记(Propertise环境变量)
- 算法笔记_103:蓝桥杯练习 算法提高 金明的预算方案(Java)
- 算法笔记_057:蓝桥杯练习 最大的算式 (Java)
- 语法练习笔记-java8
- Java学习笔记二十九:一个Java面向对象的小练习
- Java 基础一些代码练习笔记(Array二维数组)
- 算法笔记_057-蓝桥杯练习 最大的算式 (Java)
- Java 基础一些代码练习笔记(ArrayList)