1. 程式人生 > >java實現階乘的計算,丟擲異常不會,怎麼使用標號跳轉,需要幫解決一下

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);
      
      
        }
      
      
       }