重写循环为单行循环

问题描述 投票:0回答:1

是否有可能将以下循环重写为单行循环?

for (int i = 0; i < points.length; i++) {
    for (int j = i + 1; j < points.length; j++) {
        if (points[i].equals(points[j])) {
            return "Two vertices coincide therefore a polygon can't be formed";
        }

    }
}
for (int i = 0; i < points.length; i++) {
    for (int j = i + 1; j < points.length; j++) {
        if (points[i].isOnLineSegment(points[j], points[j + 1])) {
            return "There's a vertex on an edge therefore a polygon can't be formed";
        }
    }
}

我想编写发布条件,但除了可能将循环重写为一行之外,我看不到如何做到这一点。

java javadoc
1个回答
2
投票

您是否在寻找:

for (int i = 0; i < points.length; i++) {
    for (int j = i + 1; j < points.length; j++) {
        if (points[i].equals(points[j])) {
            return "Two vertices coincide therefore a polygon can't be formed";
        }
        if (points[i].isOnLineSegment(points[j], points[j + 1])) {
            return "There's a vertex on an edge therefore a polygon can't be formed";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.