我的 try-catch 在使用递归方法时陷入无限循环。

huangapple go评论84阅读模式
英文:

My try-catch is stuck in an infinite loop when using recursive method

问题

我是相当新手的,对于Java我一点都不懂,但当我强制程序由于堆栈溢出而崩溃时,我的捕获方法似乎捕获了它,但却陷入了无限循环,不知道为什么或如何修复它。有人可以帮我吗?我会非常感激。

private static int Fibonacci(int n)
{
    int fibVal = 0;
    try
    {
        if (n == 0)
        {
            fibVal = 0;
        }
        else if (n == 1)
        {
            fibVal = 1;
        }
        else
        {
            fibVal = Fibonacci(n - 1) + Fibonacci(n - 2);
        }
    }
    catch (StackOverflowError e)
    {
        System.out.println("这是另一个堆栈溢出,可能是输入太大了");
    }
}
英文:

so I'm quite new to java and I have no idea how this is happening but when I force the program to crash due to stack overflow my catch method appears to catch it but is stuck in an infinite loop, not sure why or how to fix it. Could anybody help me out? I'd really appreciate it.

private static int Fibonacci(int n)
{
    int fibVal = 0;
    try
    {
        if (n == 0)
        {
            fibVal = 0;
        }
        else if (n == 1)
        {
            fibVal = 1;
        }
        else
        {
            fibVal = Fibonacci(n - 1) + Fibonacci(n - 2);
        }
    }
    catch (StackOverflowError e)
    {
        System.out.println("This was another stack overflow, probably too high an input");
    }

答案1

得分: 1

请移除所有的try/catch块。你不应该捕获错误。

递归解法对于斐波那契数列来说非常低效。当处理小于100的数字时,你会遇到StackOverflowError。这就是你的解决方案的限制。你需要使用其他方法来实现它,你可以在网上找到相关信息。

英文:

Get rid of your try/catch blocks entirely. You're not suppose to catch errors.

https://stackoverflow.com/questions/912334/differences-between-exception-and-error

The recursive solution for Fibonacci is very inefficient. You're going to run into StackOverflowError with pretty small numbers like 100. That's just the limitation of your solution. You'd have to implement it using other methods, which you can just find online.

答案2

得分: 0

尝试这个。同意George的观点,不要在斐波那契数列中使用递归。稍微修改了你的代码。

public class Demo {

    private static int Fibonacci(int n) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else {
            return (Fibonacci(n - 1) + Fibonacci(n - 2));
        }
    }

    public static void main(String[] args) {
        int result = Fibonacci(8);
        System.out.println(result);
    }
}
英文:

Try this. Agree with George, don't use recursion for Fibonacci. Modified your code a bit.

public class Demo{

private static int Fibonacci(int n)
{
    if (n == 0)
    {
        return 0;
    }
    else if (n == 1)
    {
        return 1;
    }
    else
    {
        return (Fibonacci(n - 1) + Fibonacci(n - 2));
    }
}


     public static void main(String []args){
        int result = Fibonacci(8);
        System.out.println(result);
     }
}

答案3

得分: -1

当你捕获异常时,应该返回一个指示斐波那契调用失败的值(例如-1)。然后,你应该修改你的代码来验证斐波那契函数调用的结果,以确保一切正常,然后将它们相加。这应该解决无限循环的问题。否则,由于你正在捕获异常,递归调用将继续发生,并抛出异常。

话虽如此,由于这似乎是一项作业,只需完成任务而不必担心捕获Stackoverflow异常。此外,你可以使用-Xssn标志增加分配给虚拟机的堆栈量。

英文:

Well, when you catch the exception, you should return a value indicating that Fibonacci call failed (example -1). You should then change your code to verify the results of the Fibonacci function calls to ensure all is well, then add them. That should take care of the infinite loop. Otherwise, since you are catching the exception, recursive calls will keep happening and exceptions thrown.

That said, as this seems to be a homework, just get the thing done without worrying about catching a Stackoverflow exception. Moreover, you can increase the amount of stack allocated to the VM using the -Xssn flag.

huangapple
  • 本文由 发表于 2020年8月14日 12:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63406354.html
匿名

发表评论

匿名网友

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

确定