堆栈溢出错误,在一个方法内调用另一个方法时发生。

huangapple 未分类评论43阅读模式
英文:

Stackoverflow error while calling another method within one

问题

import java.util.Scanner;

public class lab6_Perea {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("你好!欢迎使用集合计算器。请选择您想要执行的计算:\n\t1. 指数运算\n\t2. 阶乘\n\t3. 组合\n\t4. 排列");
        int option = sc.nextInt();

        switch (option) {
            case 1:
                System.out.println("请输入底数和指数:");
                int x = sc.nextInt();
                int y = sc.nextInt();
                System.out.print("答案是:" + power(x, y));
                break;

            case 2:
                System.out.println("请输入一个数字:");
                int z = sc.nextInt();
                System.out.print(z + " 的阶乘是 " + factorial(z));
                break;

            case 3:
                System.out.println("请输入两个数字来计算组合:");
                int a = sc.nextInt();
                int b = sc.nextInt();
                System.out.print("总组合数为:" + combination(a, b));
                break;

            case 4:
                // FIX ME 调用组合方法
                break;
        }

    }

    public static int power(int n, int m) {
        if (m == 0)
            return 1;
        else
            return (n * power(n, m - 1));
    }

    public static int factorial(int n) {
        if (n == 0)
            return 1;
        else
            return (n * factorial(n - 1));
    }

    public static int combination(int n, int r) {
        int combo = factorial(n) / (factorial(r) * factorial(n - r));
        return combo;
    }
}
英文:

I'm trying to compute the combination

procedure(factorial(n)/(factorial(r)*factorial(n-r)).

When I call the factorial method from within the combination method, when running I get a stackoverflow error before the calculation.

The instructions read "This method should receive two integers as its parameters, n and r. Your combination method should call the factorial method inside its definition by using it in the following formula:"

Any help will be appreciated we haven't really covered stackoverflow so I'm a little lost. Thank you!!

import java.util.Scanner;
public class lab6_Perea{
public static void main(String[] args){
    Scanner sc = new Scanner (System.in);
    System.out.println("Hello! Welcome to Set calculator. Please select the calculation you want to perform \n\t1. Power \n\t2. Factorial \n\t3. Combination \n\t4. Permutation");
    int option = sc.nextInt();
    
    switch(option){
        case 1:
            System.out.println("Please enter the base and exponent: ");
            int x = sc.nextInt();
            int y = sc.nextInt();
            System.out.print("The answer is: " + power(x, y));
        break;
            
        case 2:
            System.out.println("Please enter a number: ");
            int z = sc.nextInt();
            System.out.print("The facotrial of " + z + " is " + factorial(z));
        break;   
            
        case 3:
            System.out.println("Please enter 2 numbers to calculate the Combination: ");
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.print("The total combination is: " + combination(a, b));
        break;
            
        case 4:
            // FIX ME call Combination method
        break;
    }
    
}
public static int power(int n, int m){
    
    if(m==0)
        return 1;
    else
        return (n * power(n, m-1));  
}

public static int factorial(int n){
    if(n == 0)
        return 0;
    else
        return (n * factorial(n-1));
}
public static int combination ( int n, int r){
    int combo =factorial(n)/(factorial(r)*factorial(n-r));
    return combo;
}

}

答案1

得分: 0

以下是翻译好的内容:

使用这个函数:

public static int factorial(int n){
    if(n <= 1)
        return 1;
    else
        return (n * factorial(n-1));
}

而且你不需要测试 n 是否为正数。你所写的递归进入了堆栈溢出,因为 n-r < 0,而你原来的规则(如果 n==0)从未被遇到。

英文:

Use this function:

public static int factorial(int n){
    if(n &lt;= 1)
        return 1;
    else
        return (n * factorial(n-1));
}

And you do not have to test n for positivity. The recursion you wrote went in StackOverflow because n-r<0 and your original rule (if n==0) was never encountered.

答案2

得分: -1

使用 return 1 if n==0

public static int factorial(int n){
    if(n == 0)
        return 1;
    else
        return (n * factorial(n-1));
}

请检查 n>=r 的条件。

希望这能够起作用!

英文:

Use return 1 if n==0

public static int factorial(int n){
    if(n == 0)
        return 1;
    else
        return (n * factorial(n-1));
}

Please check n&gt;=r condition.

Hope, this will work!

答案3

得分: -1

因为在递归中可以使用的递归次数是有限的。
为了克服这个问题,你可以在阶乘函数内部使用一个for循环。

英文:

It's because there is some limit on the number of recursions one can use.
To overcome it, you can use a for loop inside the factorial function.

huangapple
  • 本文由 发表于 2020年4月8日 11:09:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61092730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定