除以零误差
这是代码(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将其转变
为
发生的情况是
a%(b-1)
操作在b>2
测试之前执行。切换后,您将利用短路评估。一旦 b>2 测试返回 false,则无需计算模(因此避免了除法)
Try turning this around
to
What's happening is that the
a%(b-1)
operation is being executed before theb>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)
由于递归调用:
您将在某个时刻以 b 值为 1 的值调用 prime。这意味着在第二个条件下您将测试
a%0
。由于模运算符 (%) 本质上是除法,因此会带来除以零的问题。解决方案可能是捕获这种情况以强制执行 b > 。 2 在执行 % 之前根据您的情况进行操作。
Because of your recursive call:
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 %.
%的数学意义就是将
A
除以B
,这个运算的提示是C。当
B
为0
时,你实际上会问:当我们除以零时,提醒是什么?。但在数学中,除以零是未定义的,这就是 java.lang.ArithmeticException 的原因The Mathematical meaning of the
%
is that you divideA
byB
and the reminder of this operation isC
. WhenB
is0
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 forjava.lang.ArithmeticException
我假设
x % 0
形式的任何代码都会抛出此错误。您的代码无法防范这种可能性。I assume any code of the form
x % 0
will throw this error. Your code does not guard against this possibility.