Java write a program to calculate the factorial of any natural number entered by a user.
题目
Java write a program to calculate the factorial of any natural number entered by a user.
答案
import java.util.Scanner;
public class DiGui {
public static void main(String[] args){
//创建一个输入容器
Scanner input = new Scanner(System.in);
System.out.println("输入一个数:");
int grade = input.nextInt();
DiGui d = new DiGui();
int end = d.DiGuiDiaoYong(grade);
System.out.println("阶乘结果:"+end);
}
//用递归
public static int DiGuiDiaoYong(int n){
if(n==1)
return n=1;
return DiGuiDiaoYong(n-1)*n;
}
}
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
热门考点