0

这是在 main 方法里面。试图计算双 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);
}
4

2 回答 2

4

如果P小于Q(或者Q0会立即抛出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.

于 2013-09-24T22:44:18.147 回答
0

当你这样做时:

final int r = P/Q;

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

于 2013-09-24T22:44:06.383 回答