当前位置:天才代写 > tutorial > JAVA 教程 > 利用Java实现一个简朴的递归算法的实例

利用Java实现一个简朴的递归算法的实例

2017-11-13 08:00 星期一 所属: JAVA 教程 浏览:542

//CFAN 2002
//递归算法
//求阶乘
import java.io.*;
public class DiGui
{
 public static void main(String args[])
 {
  int i=0;
 char ch=' ';
 String s;
 Child ren=new Child();
 try
 {
  System.out.println("Please intput a Number,End whit '#'");
  do
  {
BufferedReader br=new BufferedReader(new InputStreamReaderSystem.in));
   
 s =br.readLine();
 i=Integer.parseInt(s);
 System.out.println(ren.Factorial(i));
  }
 while(ch!='#');
 }catch(IOException e){}
 }
}
class Child
{
double Factorial(int n)
 {
 if (n==1)  
   return 1;
 else
   return n*Factorial(n-1);
 }
}

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式