我收到java.lang.ArithmeticException错误

时间:2013-09-24 22:41:07

标签: java math

这是主要方法内部。试图计算双x数学公式,它给我运行代码的问题。有人可以帮忙吗?

final int P = Integer.parseInt(args[0]);
final int Q = Integer.parseInt(args[1]);
final double H = Double.parseDouble(args[2]);
final int N = Spiro.numberOfRevolutions(P, Q);

IPoint2D point = new IPoint2D(0., 0.);
SimpleDrawing.drawPoint(point);

for (int i = 1; i <= 360*N; i++) {
    final int r = P/Q;
    double radianI = Math.toRadians(i);
    double x = ((1 - r) * Math.cos(radianI)) + (H * Math.cos(((1- r) / r) *  radianI));
    double y = ((1 - r) * Math.sin(radianI)) + (H * Math.sin(((1- r) / r) * radianI));

    IPoint2D point1 = new IPoint2D(x,y);
    SimpleDrawing.drawPoint(point1);
}

2 个答案:

答案 0 :(得分:4)

如果P小于Q(或Q 0会立即抛出ArithmeticException

final int r = P/Q;

r的值为0。然后除以r=0

double x = ((1 - r) * Math.cos(radianI)) + (H * Math.cos(((1- r) / r) *  radianI));

抛出ArithmeticException

答案 1 :(得分:0)

当你这样做时:

final int r = P/Q;

你需要确保Q永远不为零,否则它将是ArithmeticException