【问题标题】:Runtime error Arithmetic Exception divide by zero运行时错误算术异常除以零
【发布时间】:2018-07-01 04:48:25
【问题描述】:

这是运行时错误

Exception Name: java.lang.ArithmeticException
Exception Message: / by zero

我怎样才能避免这种情况?

 else if(((arr1[3]-arr1[1])/(arr1[2]-arr1[0]))==-1/((arr2[3]-arr2[1])/(arr2[2]-arr2[0]))){
            System.out.println("YES");

【问题讨论】:

  • 使用 try catch 块。在第一个 catch 块中捕获 ArithematicException,然后在其后的下一个 catch 块中捕获 Exception。
  • 我已经这样做了,仍然没有通过显示运行时错误 try{ if((arr1[3] == arr1[1] && arr1[2]== arr1[0]) || (arr2 [3] == arr2[1] && arr2[2]== arr2[0])){ System.out.println("INVALID");否则 if(((arr1[3]-arr1[1])/(arr1[2]-arr1[0]))==-1/((arr2[3]-arr2[1])/(arr2[2 ]-arr2[0]))){ System.out.println("YES"); }else{ System.out.println("NO"); } } catch(ArithmeticException e){ System.out.println("NO"); }
  • “我怎样才能避免这种情况?” 不要试图除以零,即当arr1[2] == arr1[0] arr2[3] == arr2[1] arr2[2] == arr2[0].
  • @risabh_95 不要在评论中发布代码。 编辑问题并在那里发布代码以澄清问题。

标签: java divide-by-zero


【解决方案1】:

你的分母结果为零。如果除法的分母为零值,请处理这种情况。

【讨论】:

  • 我已经处理过了,仍然显示错误 try{ if((arr1[3] == arr1[1] && arr1[2]== arr1[0]) || (arr2[3] == arr2[1] && arr2[2]== arr2[0])){ System.out.println("INVALID"); } else if(((arr1[3]-arr1[1])/(arr1[2]-arr1[0]))==-1/((arr2[3]-arr2[1])/(arr2[ 2]-arr2[0]))){ System.out.println("YES"); }else{ System.out.println("NO"); } } catch(ArithmeticException e){ System.out.println("NO");}
猜你喜欢
  • 2014-06-27
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
相关资源
最近更新 更多