java 我的求四边形参数的方法总是返回0 [closed]

iyzzxitl  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(104)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
4小时前关门了。
Improve this question
我对Java还是个新手,不知道哪里出了问题。

import java.awt.geom.Point2D;

public class Quadrilaterals {

    private Point2D[] vertices;

    public Quadrilaterals(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
        vertices = new Point2D[4];
        vertices[0] = new Point2D.Double(x1, y1);
        vertices[1] = new Point2D.Double(x2, y2);
        vertices[2] = new Point2D.Double(x3, y3);
        vertices[3] = new Point2D.Double(x4, y4);
    }

    public double getPerimeter() {
        double perimeter = 0;
        for (int i = 0; i < 4; i++) {
            perimeter += vertices[i].distance(vertices[(i + 1) % 4]);
        }
        return perimeter;
    }

我不知道如何让它返回0以外的值。

pkwftd7m

pkwftd7m1#

如果我这样调用它,它将打印16.970562748477143

Quadrilaterals q = new Quadrilaterals(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
System.out.println(q.getPerimeter());

相关问题