import java.util.*;
public class jiechen15{//计算一个整数阶乘类
private static final int A=15;//定义常量A
private static final int B=11;//定义常量B
//For循环计算阶乘
public static int simpleCircle(int num){
int sum=1;
if(num<0){//判断传入数是否为负数
System.out.println("please enter a negative number!");//抛出不合理参数异常
break flag;//跳回标记、、、不能跳啊 帮解决一下
}
for(int i=1;i<=num;i++){
sum *= i;//每循环一次进行乘法运算
}
return sum;//返回阶乘的值
}
//递归计算阶乘
public static int reCircle(int num){
int sum=1;
if(num<0){
System.out.println("please enter a negative number!");
return 0;
break flag;//跳回标记、、、不能跳啊 帮解决一下
}
if(num==0)
return 1;//0!=1
if(num==1)
return 1;
else
{
sum=num*reCircle(num-1);//递归
return sum;
}
}
//程序入口
public static void main(String[] args){
int m=1;//控制do-while语句
System.out.println("For循环计算常量"+A+"的阶乘为:"+simpleCircle(A));//for循环计算常量A的阶乘
System.out.println("递归计算常量"+B+"的阶乘为"+reCircle(B));
do{
System.out.println("please choose function:");
System.out.println("1.For循环 ");
System.out.println("2.递归");
System.out.println("0.退出");
Scanner sc=new Scanner(System.in);
flag://标号
System.out.print("please enter a number to caculate:");
int i=sc.nextInt();
System.out.print("choose method to caculate:");
int c=sc.nextInt();
switch(c){
case 1:System.out.println("For循环计算"+i+"的阶乘为:"+simpleCircle(i));//调用For循环算法
break;
case 2:System.out.println("递归计算"+i+"的阶乘为:"+reCircle(i));//调用递归算法
break;
case 0: System.out.println("Wherecom to use!");
m=0;
}
}while(m==1);
}
}