首页 > 编程语言 > 详细

Java递归算法——阶乘

时间:2016-04-06 15:01:18      阅读:259      评论:0      收藏:0      [点我收藏+]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

//=================================================
// File Name       :	Factorial_demo
//------------------------------------------------------------------------------
// Author          :	Common

//类名:
//属性:
//方法:


//主类
//Function        : 	triangle_demo
public class Factorial_demo {

	public static void main(String[] args) throws Exception{
		// TODO 自动生成的方法存根
		System.out.println("输入数字:");
		int theNumber = getInt();
		int theAnswer = factorial(theNumber); 
		System.out.println("阶乘:"+theAnswer);
	}
	
	public static int factorial(int n){		//递归
		if(n == 1)
			return 1;
		else
			return (n*factorial(n-1));
	}
	
	//输出方法
		public static String getString() throws IOException{
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);
			String s = br.readLine();
			return s;
		}
		
		//输出方法
		public static int getInt() throws IOException{
			String s = getString();
			return Integer.parseInt(s);
			
		}

}

 

Java递归算法——阶乘

原文:http://www.cnblogs.com/tonglin0325/p/5359200.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!