java实现阶乘的计算,抛出异常不会,怎么使用标号跳转,需要帮解决一下

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
      
      
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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值