为什么我在这里得到java.lang.StackOverflowError?

Suh*_*pta 1 java stack-overflow recursion exception factorial

_以下问题的以下程序给出了一系列例外Exception in thread "main" java.lang.StackOverflowError at testing_package.Compute.factorial(Compute.java:105)我不明白为什么会出现此错误.

问题:N个男孩和M个女孩正在从剧院学习表演技巧.为了表演,他们需要组成一组P演员,其中包含不少于4个男孩和不少于1个女孩.剧院要求你写一个程序,告诉他们组的形成方式.注意:组成应该是唯一的,而不是组成的顺序.

import java.io.*;

class Compute {

private static int NFact;
private static int N_Minus_R_Fact;
private static int RFact;
private static int fact=0;

public static int readTheStrengthOfGroup() {
    int strengthOfGroup=0;
    try {
        System.out.println("Enter the strength of group : ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String read = reader.readLine();
        strengthOfGroup = Integer.parseInt(read);
    } catch(Exception exc) {
        System.out.println(exc);
      }
      return strengthOfGroup;
}

public static int readTheNumberOfBoys() {
   int boysToParticipate=0;
   try {
    System.out.println("Enter the number of boys to participate in the play : ");
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String read = reader.readLine();
    boysToParticipate = Integer.parseInt(read);
   } catch(Exception exc) {
        System.out.println(exc);
     }
    return boysToParticipate;
}

public static int readTheNumberOfGirls() {
    int girlsToParticipate=0;
    try {
        System.out.println("Enter the number of girls to participate in the play : ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String read = reader.readLine();
        girlsToParticipate = Integer.parseInt(read);
    } catch(Exception exc) {
        System.out.println(exc);
      }
    return girlsToParticipate;
}

public static int compute(int strengthOfGroup , int boysToParticipate , int girlsToParticipate) {
    if( boysToParticipate < 4 || girlsToParticipate < 1) {
        return 0;
    } else {
        /*  P >= 5 
         *  N : Boys
         *  M : Girls
         *  result = M+N C P - { (N C 0)(M C P)+(N C 1)(M C P-1)+(N C 2)(M C P-2)+(N C 3)(M C P-3)+(N C P)(M C 0) }
         */
         int resultP_2 = 0;
         int totalNumberOfParticipants = boysToParticipate + girlsToParticipate;
         int totalNumberOfParticipants_C_strengthOfGroup = computeFactorial(totalNumberOfParticipants , strengthOfGroup);
         for( int i = 0 ; i <= 4 ; i++ ) {
                          if( i == 4 ) {
                resultP_2 = resultP_2 + (computeFactorial(boysToParticipate,strengthOfGroup) * computeFactorial(girlsToParticipate,0)); 
            }else {
            resultP_2 = resultP_2 + (computeFactorial(boysToParticipate,i) * computeFactorial(girlsToParticipate,strengthOfGroup));
            strengthOfGroup--;}
         }
         int result = totalNumberOfParticipants_C_strengthOfGroup - resultP_2;
         return result;
      }
}

public static int computeFactorial(int N , int R) {
    if(R > N) {
        throw new RuntimeException("Invalid Parameters");
    } else {
        /* int NFact;
        int N_Minus_R_Fact;
        int RFact; */
        NFact = factorial(N);
        N_Minus_R_Fact = factorial(N-R);
        RFact = factorial(R);
        return( NFact / ( N_Minus_R_Fact-RFact ) );

      }
}

public static int factorial(int num) {
    if( num == 1 ) {
        return 1;
    } else {
        fact = num * factorial(num-1); // LINE 105
        return fact;
      }
}

public static void main(String args[]) {
    int strengthOfGroup = readTheStrengthOfGroup();
    int boysToParticipate = readTheNumberOfBoys();
    int girlsToParticipate = readTheNumberOfGirls();
    int result = compute(strengthOfGroup , boysToParticipate , girlsToParticipate);
    System.out.println("Number of groups that can be formed : " + result);
}
Run Code Online (Sandbox Code Playgroud)

}

我已经注释了第105行. 在此输入图像描述

T.J*_*der 6

computeFactorial避免调用factorialif R > N,但在所有其他情况下(R == N,R < N)调用它,传入N-R.如果R == N,然后N-R0.在factorial,你如果检查num == 1和恢复1,但是当num0,你有factorial叫本身num - 1,这是-1.然后调用自身再次num - 1,这是-2等与不断日益大型负数(-1,-2,...),直到你用完栈.

我还没有仔细阅读代码,但最起码,你需要有factorial回报1num == 0,以及当num == 1(0! = 1).如果你给它一个负数,我也会抛出异常.