除以零误差

发布于 2024-08-24 07:56:06 字数 988 浏览 4 评论 0原文

这是代码(java):

class prime
{

    public static boolean prime (int a, int b) 
    { 
        if (a == 0) 
        {
            return false; 
        }
        else if ((a%(b-1) == 0) && (b>2))
        {
            return false; 
        }
        else if (b>1) 
        {
            return (prime (a, b-1)) ;
        }
        else
        {
            return true; 
        }

    }

    public static void main (String[] arg) 
    {
        System.out.println (prime (7, 7)) ; 
    }
}

这是我尝试运行它时收到的错误消息(它编译得很好):

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at prime.prime(prime.java:10)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.main(prime.java:27)

所以这意味着我除以零,对吗?或者还有别的意思吗?我不明白我是如何除以零的。出了什么问题?

here is the code (java):

class prime
{

    public static boolean prime (int a, int b) 
    { 
        if (a == 0) 
        {
            return false; 
        }
        else if ((a%(b-1) == 0) && (b>2))
        {
            return false; 
        }
        else if (b>1) 
        {
            return (prime (a, b-1)) ;
        }
        else
        {
            return true; 
        }

    }

    public static void main (String[] arg) 
    {
        System.out.println (prime (7, 7)) ; 
    }
}

This is the error message i get when i try to run it (it compiles fine):

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at prime.prime(prime.java:10)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.prime(prime.java:16)
    at prime.main(prime.java:27)

So this means i devided by zero some how right? or does it mean something else? I don't see how i'm dividing by zero. What went wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江挽川 2024-08-31 07:56:06

尝试将其转变

if ((a%(b-1) == 0) && (b>2))

   if ((b>2) && a%(b-1)==0)

发生的情况是 a%(b-1) 操作在 b>2 测试之前执行。

切换后,您将利用短路评估。一旦 b>2 测试返回 false,则无需计算模(因此避免了除法)

Try turning this around

if ((a%(b-1) == 0) && (b>2))

to

   if ((b>2) && a%(b-1)==0)

What's happening is that the a%(b-1) operation is being executed before the b>2 test.

After the switch, you are taking advantage of short-circuit evaluation. Once the b>2 test returns false, then there's no need to calculate the modulus (hence avoiding the division)

最美的太阳 2024-08-31 07:56:06

由于递归调用:

return (prime (a, b-1)) ;

您将在某个时刻以 b 值为 1 的值调用 prime。这意味着在第二个条件下您将测试 a%0。由于模运算符 (%) 本质上是除法,因此会带来除以零的问题。

解决方案可能是捕获这种情况以强制执行 b > 。 2 在执行 % 之前根据您的情况进行操作。

Because of your recursive call:

return (prime (a, b-1)) ;

You will at some point be calling prime with a value for b of 1. Which means on your second condition you will be testing a%0. Since the modulo operator (%) is essentially a divide, that is bringing your divide by zero issue.

The solution is probably to catch this case to enforce b > 2 in your condition before doing the %.

可爱咩 2024-08-31 07:56:06
 A % B = C

%的数学意义就是将A除以B,这个运算的提示是C。当B0时,你实际上会问:当我们除以零时,提醒是什么?。但在数学中,除以零是未定义的,这就是 java.lang.ArithmeticException 的原因

 A % B = C

The Mathematical meaning of the % is that you divide A by B and the reminder of this operation is C. When B is 0 you effectively ask : What is the reminder when we divide by zero ?. In mathematics though, division by zero is undefined and this is the reason for java.lang.ArithmeticException

Smile简单爱 2024-08-31 07:56:06

我假设 x % 0 形式的任何代码都会抛出此错误。您的代码无法防范这种可能性。

I assume any code of the form x % 0 will throw this error. Your code does not guard against this possibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文